1

I'm having System.OutOfMemory exceptions in my .NET Windows Service. I'm not sure what's causing it. I suspect fragmentation in the large object heap but i'm not sure. How can I verify my hypothesis?

I can use debuggers of all kinds, but what in the debugger should I look for?

Thanks

Mark
  • 5,223
  • 11
  • 51
  • 81
  • 1
    Have you initially tried debugging the source code for anything obvious? – Grant Thomas Mar 03 '11 at 19:33
  • 1
    Well, it's a given, you never get OOM on itty-bitty objects. They always fit somewhere. Use a memory profiler. Test your service with real production data. It will be twice as big next year, cover that too. – Hans Passant Mar 03 '11 at 19:56

2 Answers2

2

CLR Profiler is your friend for this. It can attach to a Windows Service and gather all the metrics you need to find what's the culprit when your OOM hits.

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • What should I look for in the CLR Profiler that will let me know whether the cause is the large object heap or not? – Mark Mar 03 '11 at 20:16
  • For your particular scenario, I'd say start with the Final Heap Bytes histogram and then find what type of object is soaking up your memory. From there, you can right-click and select Show Who Allocated and find which methods are allocating those large objects. – Jesse C. Slicer Mar 03 '11 at 20:30
  • If the large object heap never grows above 60 MB, can it still be the cause of my out of memory exception – Mark Mar 04 '11 at 21:30
  • IF you have a lot of objects that get allocated and freed from it, YES. The LOH is never compacted, so gaps could occur and there may be a point where just one more object can't fit in the gaps available. – Jesse C. Slicer Mar 04 '11 at 22:09
  • @Jesse C. Slicer - but if that was too happen, wouldn't the LOH appear large since .NET framework would expand it in order to fit the large object that currently can't fit? Or does does the large object heap counters only show the occupied space and not the free space? – Mark Mar 08 '11 at 19:16
  • @Jesse C. Slicer - "Large object heap size" counter is the current size in bytes, including the free space, of the Large Object Heap. So if the large object heap was causing an out of memory exception to be thrown you would expect the LOH to be pretty large would you not? – Mark Mar 08 '11 at 19:17
  • As I said - you can have (for example) 1GB free in the LOH, but the largest single chunk may be 200K. If you try to allocate a 250K object, the 1GB free means nothing - there's no space big enough to hold it. Search for "LOH fragmentation" for more information. – Jesse C. Slicer Mar 08 '11 at 19:57
0

I guess this happens randomly and never when you are debugging on your machine, right?

What you can do is to create a dump and analyse the remains.

You need to turn to the true master of debugging, Tess. She is the Chuck Norris of debugging.

Check this out, for example.

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79