If your .NET application displays the 'System.OutOfMemoryException' error message, you might assume it is a server memory issue, but that is not the case.
The error is related to contiguous blocks of memory in a 32-bit process.
It is caused by:
- Your process is using too much memory (typically over 800 MB in a 32-bit environment).
- The virtual address space is fragmented, reducing the likelihood that a large, contiguous allocation will succeed.
This is explained in the following Microsoft support article:
When an application needs to use memory, it reserves a chunk of the virtual address space and then commits memory from that chunk. It is exactly what the .NET Framework's garbage collector (GC) does when it needs memory to grow the managed heaps. When the GC needs a new segment for the small object heap (where objects smaller than 85 KB reside), it makes an allocation of 64 MB. When it needs a new segment for the large object heap, it makes an allocation of 16 MB. These large allocations must be satisfied from contiguous blocks of the 2 GB of address space that the process has to work with. If the operating system is unable to satisfy the GC's request for a contiguous block of memory, a System.OutOfMemoryException (OOM) occurs.
The quickest way to get your website back online is to recycle the application pool. This will clear the address space.
It won't, however, solve the underlying issue. You can refer to this Stack Overflow article for more solutions on how to fix your website: