2010-10-12 97 views
3

我正在使用此WCF-WPF應用程序。我經常在異步XamDataGrid上收到此錯誤。嘗試進行WCF調用並填充網格。我最初認爲這與從WCF返回的大量數據有關,我通過僅爲一個類別調用,使得調用返回數據非常小。這個錯誤隨機出現,並不總是在同一組數據上。在15-20次嘗試中足夠可複製。WPF WCF MVVM OutOfMemoryException

我正在運行Windows XP(32位),雙核,內存爲4GB。當發生這種異常時,客戶端機器僅使用大約2GB的RAM,而在服務器上W3WP僅使用800MB(6GB,3個內核,服務器上的總內存使用量爲〜2GB)。

這隻發生在XP機器上。在Windows 7上不會出現此錯誤。

請指導我如何解決此問題。

在此先感謝

Event Viewer Logs this Message: 
Event Type: Error 
Event Source: .NET Runtime 2.0 Error Reporting 
Event Category: None 
Event ID: 5000 
Date:  10/13/2010 
Time:  10:50:07 AM 
User:  N/A 
Computer: COMP-DC7800 
Description: 
EventType clr20r3, P1 appname.exe, P2 2.0.0.21872, P3 4cb0a1b1, P4 mscorlib, P5 2.0.0.0, P6 492b834a, P7 35df, P8 45, P9 system.outofmemoryexception, P10 NIL. 

下面ID異常的詳細信息:

System.OutOfMemoryException was unhandled 
Message="Exception of type 'System.OutOfMemoryException' was thrown." 
Source="mscorlib" 
StackTrace: 
    at System.IO.MemoryStream.set_Capacity(Int32 value) 
    at System.IO.MemoryStream.EnsureCapacity(Int32 value) 
    at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
    at System.Xml.XmlMtomReader.MimePart.GetBuffer(Int32 maxBuffer, Int32& remaining) 
    at System.Xml.XmlMtomReader.Initialize(Stream stream, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize) 
    at System.Xml.XmlMtomReader.SetInput(Stream stream, Encoding[] encodings, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize, OnXmlDictionaryReaderClose onClose) 
    at System.Xml.XmlMtomReader.SetInput(Byte[] buffer, Int32 offset, Int32 count, Encoding[] encodings, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize, OnXmlDictionaryReaderClose onClose) 
    at System.ServiceModel.Channels.MtomMessageEncoder.MtomBufferedMessageData.TakeXmlReader() 
    at System.ServiceModel.Channels.BufferedMessageData.DoTakeXmlReader() 
    at System.ServiceModel.Channels.BufferedMessageData.GetMessageReader() 
    at System.ServiceModel.Channels.MessageHeaders.GetBufferedMessageHeaderReaderAtHeaderContents(IBufferedMessageData bufferedMessageData) 
    at System.ServiceModel.Channels.MessageHeaders.GetBufferedMessageHeaderReader(IBufferedMessageData bufferedMessageData, Int32 bufferedMessageHeaderIndex) 
    at System.ServiceModel.Channels.MessageHeaders.GetReaderAtHeader(Int32 headerIndex) 
    at System.ServiceModel.Channels.WsrmMessageInfo.Get(MessageVersion messageVersion, ReliableMessagingVersion reliableMessagingVersion, IChannel channel, ISession session, Message message, Boolean csrOnly) 
    at System.ServiceModel.Channels.ReliableDuplexSessionChannel.HandleReceiveComplete(IAsyncResult result) 
    at System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnReceiveCompletedStatic(IAsyncResult result) 
    at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) 
    at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously) 
    at System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputComplete(IAsyncResult result) 
    at System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputCompleteStatic(IAsyncResult result) 
    at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) 
    at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously) 
    at System.ServiceModel.Channels.InputQueue`1.AsyncQueueReader.Set(Item item) 
    at System.ServiceModel.Channels.InputQueue`1.Dispatch() 
    at System.ServiceModel.Channels.InputQueue`1.OnDispatchCallback(Object state) 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2() 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.OnSecurityContextCallback(Object o) 
    at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state) 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke() 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks() 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state) 
    at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) 
    at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped) 
    at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) 
+0

如果你看電腦屬性,它實際上報告4GB?在使用2GB內存時,您的流程需要多少錢? – 2010-10-12 17:22:30

+0

是的..這是使用的總內存。該過程僅使用〜400MB – Bhuvan 2010-10-12 18:57:25

+0

您是否檢查過應用程序是否存在內存泄漏? – decyclone 2010-12-14 06:50:56

回答

0

感覺放心找到答案,微軟搞砸了所有...... :)。那責備幾乎救了我的工作......這是故事.. WCF有64kb的默認消息大小。在MSDN上找不到這個原因..所以當我們需要將最大消息大小更改爲我們想要的。問題是threshold for LOH是85000b。如果消息大小超過該值,則該對象將被放置在LOH中,分析器將顯示對象已經收集到,但是當您在Sysinternals Process Explorer,Process properties窗口中看到LOH大小時,仍然會看到內存使用正在增加。在800MB左右,我們強大的沃森博士出現並開始使用這個應用程序。顯然,這是.NET 2.0

問題在.NET 4.0中仍然存在,但微軟發佈了quickfix的已知問題WCF &。 Sysinternals Process Explorer是幫助診斷這個問題的工具。見屏幕截圖

+0

以某種方式附加一個PNG圖像不工作.. – Bhuvan 2011-01-06 18:38:31

0

您將無法利用了4 GB與32位操作系統的RAM。

http://chris.pirillo.com/32-bit-windows-and-4gb-of-ram/

如果你使用你的應用程序正在消耗越來越多的內存,你可能想看看你的內存管理。查看爲非託管資源的類實現IDisposable接口。另外,請避免在完成使用後保留對象的引用。事件對此尤其不利。

How do events cause memory leaks in C# and how do Weak References help mitigate that?

你也可以看看串流到一個臨時文件,而不是流於記憶。

+1

@謝謝邪惡的鴿子{感到很不舒服,感謝一個邪惡的鴿子..犯罪的大多數宗教: )..你應該重新命名爲天使鴿子} ..我檢查了由於綁定,引用和「失敗的WPF綁定」使用VS2010 Profiler(它真棒)的泄漏..它是非常小..在2MB以下..我我不知道這是否導致崩潰..我在邪惡的沃森博士出現之前看到的穗是600MB ..應該有其他東西.. – Bhuvan 2010-12-15 14:21:16

1

OutOfMemory異常可能會出現在很多原因中,特別是在Web應用程序中。

首先,如果你正在運行在IIS上,有具體到IIS設置來限制內存,在這裏看到:http://blogs.msdn.com/b/pfedev/archive/2009/01/22/memory-based-recycling-in-iis-6-0.aspx

根據您的配置,游泳池等,這些限制可能不適用於.NET web應用程序。現在

,在ASP.NET中,也有一個設置的memoryLimitmachine.config中文件,指定允許的最大內存大小,系統內存總量的百分比,工作進程可以消耗之前ASP.NET啓動一個新進程並重新分配現有的請求。

默認值是60%,相當於2G機器上的1.2G。

然而,...無論您配置,800M是一個32位的機器上ASP.NET應用程序的實際限制,在這裏看到了很好的解釋:Understanding ASP.Net memory

避免這個問題的解決辦法是改變方式您的系統工作並且在更小的片斷。

這就是爲什麼默認情況下,WCF已經被認真配置了很多限制(請參閱這裏的廣泛列表:http://weblogs.asp.net/paolopia/archive/2008/03/23/wcf-configuration-default-limits-concurrency-and-scalability.aspx)......幾乎每個開發人員一旦發現它就會改變和最大化: )

+0

。問題在於WCF客戶端。服務可以正常運行幾個星期沒有任何問題..內存使用固定在一個千兆..可能是因爲我主持靜態引用的客戶端會話的雙工通信。但這不是現在的問題..該應用程序是WCF-WPF-MVVM的Windows客戶端。 – Bhuvan 2010-12-15 14:14:25

+1

客戶端或服務器,出於與解釋相同的原因,32位XP機器上的.NET進程的實際限制大約爲800M。另一個鏈接:http://stackoverflow.com/questions/934314/maximum-memory-a-net-process-can-allocate。 WPF/MVVM可能與您的問題無關。您需要更改構建WCF服務接口的方式。 – 2010-12-15 17:18:17