Issues related to high memory utilization on an IIS application server are common. With .NET there is a little misconception that the Garbage Collector (GC) will clean up objects and therefore the process can never run out of memory. This isn’t true. GC will never clean up an object which is in use. If that was the case, you can imagine the kind of problems it would create.
While debugging memory problems, it is a good idea to capture memory dump when the process memory consumption is at its peak maximum usage. For .NET applications, a System.OutOfMemoryException is thrown when GC fails on a VirtualAlloc().
So how do we capture a memory dump when this Exception is thrown? Here’s how.
For .NET Framework version 1.1
Open the registry path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework Key: GCFailFastOnOOM Type: DWORD Value: 2
Open the registry path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework
Key: GCFailFastOnOOM
Type: DWORD
Value: 2
For .NET Framework version 2.0 and above
Open the registry path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework Key: GCBreakOnOOM Type: DWORD Value: 2
Key: GCBreakOnOOM
Setting the above key causes a DebugBreak within the process when a System.OutOfMemoryException is encountered. You can then use a tool like DebugDiag or a Debugger like WinDBG/CDB/NTSD to capture a dump on this DebugBreak Exception. Windbg/CDB/NTSD debuggers are for advanced users and DebugDiag is generally preferred due to ease of use and is designed to be used in production environments.
Configuring Debug Diagnostic Tool
NOTE: When the dump is captured, the Userdump count column will be incremented by 1. You can then do post mortem debugging using Windbg and SOS.