3

I am working on this WCF-WPF app. I am frequently getting this error on an asynchronous XamDataGrid. Trying to make a WCF call and populate the Grid. I initially thought this is something to do with the large amounts of data returned from WCF and I made the call return data is much small pieces, by calling only for a category only. This error comes up randomly, not always on the same set of data. Reproducible enough in 15-20 tries.

I am running Windows XP (32bit), Dual Core, with 4GB of ram. When this exception is raised, client machine only uses about 2GB of RAM, and on the server W3WP is using only 800MB (of 6GB, 3 cores. Total memory use on server is ~2GB).

This is only happening on XP machines. Does not get this error on Windows 7 box.

Please guide me how to resolve this issue.

Thanks in advance

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.

Below id the Exception details:

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)
sisve
  • 19,501
  • 3
  • 53
  • 95
Bhuvan
  • 1,523
  • 4
  • 23
  • 49
  • If you look at computer properties does it actually report 4GB? And of the 2GB of RAM used, how much is dedicated to your process? – Kent Boogaart Oct 12 '10 at 17:22
  • yes.. It is the total memory used. the process is using only using ~400MB – Bhuvan Oct 12 '10 at 18:57
  • Have you checked you application for memory leaks? – decyclone Dec 14 '10 at 06:50
  • @Bhuvan 1) Does the memory starts going up gradually until it runs out or it is all fine and suddenly it happens within the frame of one WCF call? 2) How big is your XML? – Aliostad Dec 15 '10 at 12:48
  • @Aliostad... Yes the memory goes up gradually on the client.. for each Service call.. Service uses MTOM encoder and returns about 1MB (int he ball park) per call, which are essentially Lists of Business Objects. Sometimes Memory getting released.. when I watch in Process Explorer.. but the pattern is consistent that after 50-60 service calls the client just bombs out.. – Bhuvan Dec 15 '10 at 14:11
  • @Buhvan I assume it is a WPF/Financial so you might be doing some clever graph stuff in there as well. Are you loading, drawing, rendering any images in there? – Aliostad Dec 15 '10 at 14:16
  • I am calling GC.Collect() after each service call and the memory use drops .. monitored using "Process Explorer", still the app crashes – Bhuvan Dec 15 '10 at 14:24

3 Answers3

1

OutOfMemory exceptions can happen for a lot of reasons in general, and in web apps in particular.

First, if you're running on IIS, there are settings specific to IIS to limit memory, see here: http://blogs.msdn.com/b/pfedev/archive/2009/01/22/memory-based-recycling-in-iis-6-0.aspx

Depending on your configuration, pools, etc., these limits may not apply to .NET web applications.

Now, in ASP.NET, there is also a setting memoryLimit in the machine.config file that specifies the maximum allowed memory size, as a percentage of total system memory, that the worker process can consume before ASP.NET launches a new process and reassigns existing requests.

The default is 60%, which is equal to 1.2G on a 2G machine.

However,... whatever you configure, 800M is the practical limit for ASP.NET apps on a 32-bit machine, see a good explanation here: Understanding ASP.Net memory

The solution to avoid this is to change the way your system works and break it in smaller pieces.

That's why by default, WCF has been carefully configured with so many limits (see here for an extensive list: http://weblogs.asp.net/paolopia/archive/2008/03/23/wcf-configuration-default-limits-concurrency-and-scalability.aspx) ... which almost every developer tends to change and max-out as soon as he discovers it :-)

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • . the issue is with the WCF Client. Service runs fine for weeks without any issue.. with Memory usage pinned at around a Gig .. may be because I am hosting static references to the the Client Sessions for duplex comm. But that's not the problem now.. The App is WCF-WPF-MVVM windows client. – Bhuvan Dec 15 '10 at 14:14
  • 1
    Client or Server, the practical limit for a .NET Process on a 32-bit XP machine is around 800M for the same reasons as explained. Another link: http://stackoverflow.com/questions/934314/maximum-memory-a-net-process-can-allocate. WPF/MVVM are probably irrelevant to your issue. You need to change the way you build your WCF services interfaces. – Simon Mourier Dec 15 '10 at 17:18
0

You won't be able to utilise that 4 GB of RAM with a 32 bit OS.

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

If your application is consuming more and more memory as you use it, you may want to look at your memory management. Look at implementing the IDisposable interface for classes with unmanaged resources. Also, avoid keeping references to objects once you've finished using them. Events are particularly bad for this.

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

You could also look at streaming to a temporary file instead of streaming to memory.

Community
  • 1
  • 1
Evil Pigeon
  • 1,887
  • 3
  • 23
  • 31
  • 1
    @Thanks Evil Pigeon {little uncomfortable to thank an Evil Pigeon.. offending most religions :).. u should probably be renamed to Angel Pigeon} .. I checked the leaks due binding, references, and "Failed WPF bindings" using VS2010 Profiler (it's awesome).. and it is very minimal.. under 2MB.. I am not sure if this is causing the crash.. The Spike I am seeing before the Evil Dr Watson appears is like 600MB.. There should be something else.. – Bhuvan Dec 15 '10 at 14:21
0

Feel Relieved to find the answer that Microsoft Screwed it up all... :). that blame almost saved my Job... Here is the story.. WCF has a default message size of 64kb. One cannot find the reason for this on MSDN.. and so when we need to change the max message size to whatever we want. The problem is that the threshold for LOH is 85000b. If the message size is more than that, the object will be placed in LOH, your profiler will show that the objects have collected.. but when you see the LOH size in Sysinternals Process Explorer, Process properties window, you still see that the memory using is increasing. At around 800MB, our mighty Dr Watson comes up and kick of this application. Apparently this is a known issue with WCF & .net 2.0

Issue still exists in .net 4.0 but Microsoft released a quickfix. Sysinternals Process explorer is the tool which helped diagnose this issue. See Screenshot

Bhuvan
  • 1,523
  • 4
  • 23
  • 49