Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Out of memory exception - warning strategy

Inside a complex multithreaded application I am hitting an out-of-memory exceptions maybe once a week. The application is sending/reading massive amounts of data via several sockets, where the read data gets cached to avoid network card buffer overruns.
What is the best strategy to analyse the memory exceptions? During normal run-time, the App is shown with a size of "Total bytes in all Heaps" of up to 1.5 Gigabyte in the Process explorer.
Could it be a strategy to have a thread which is polling either

GC.GetTotalMemory()

or

PrivateMemorySize64()

once a second to know when to start analyzing things? I have not looked into commercial profilers yet and I am a bit concerned of the performance impact of them which could give also wrong results regarding the actual problem analysis.

like image 970
weismat Avatar asked Feb 01 '12 08:02

weismat


2 Answers

Your memory is probably being fragmented from numerous string operations or other operations that create and release small blocks of memory, such as boxing/unboxing.

You will get this exception when the CLR cannot allocate a large enough free block of memory.

I use the "CLR Profiler" and check the memory allocations. If you see numerous white-spots (free blocks) and no large free blocks then you need to start looking at how you are allocating objects.

For instance, before assigning one string to another, check to see if the strings are different first. Using StringBuilder is all cases, eliminate boxing, and other memory optimizations.

I use this technique and completely eliminated the exceptions, except for a known issue with binary de-serialization.

Rediscover the Lost Art of Memory Optimization in Your Managed Code at http://msdn.microsoft.com/en-us/magazine/cc163856.aspx

Investigating Memory Issues at http://msdn.microsoft.com/en-us/magazine/cc163528.aspx

Performance Optimization in Visual Basic .NET at http://msdn.microsoft.com/en-us/library/aa289513(v=vs.71).aspx

like image 65
AMissico Avatar answered Nov 07 '22 18:11

AMissico


You might consider installing the debugging tools for windows and using adplus

ADPlus.vbs (ADPlus) is a tool from Microsoft Product Support Services (PSS) that can troubleshoot any process or application that stops responding (hangs) or fails (crashes).

Basically, you can set that watching the application, and when it crashes, it will capture a dump, that you can then analyse using WinDBG/SOS.

like image 27
Damien_The_Unbeliever Avatar answered Nov 07 '22 17:11

Damien_The_Unbeliever