Error AtlasError Documentation and Resolution

.NET runtime threw OutOfMemoryException

System.OutOfMemoryException: Insufficient memory to continue the execution of the program.

The .NET runtime threw an OutOfMemoryException because it could not allocate the requested memory from the managed heap or the process address space. On 32-bit processes this can occur well before physical RAM is exhausted due to the 2–4 GB virtual address space limit. On 64-bit processes it usually indicates a genuine memory exhaustion problem — a memory leak, unexpectedly large allocations, or fragmentation of the Large Object Heap (LOH).

OutOfMemoryExceptionruntime
System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
  • A memory leak — objects are being held alive unintentionally (e.g., event handler subscriptions, static collections, unclosed streams or connections) preventing the GC from collecting them.
  • Running as a 32-bit process, which hits the virtual address space limit (typically 1.4–2 GB for managed heap) before physical RAM is exhausted.
  • Large Object Heap (LOH) fragmentation caused by frequent allocation and deallocation of large objects (>85,000 bytes), which are not compacted by default.
  • A single operation allocating an extremely large array or collection that exceeds available contiguous memory.
  • Loading very large files, images, or datasets entirely into memory rather than streaming them.
  • The system is genuinely out of physical and virtual memory due to overall system load.
  1. Profile memory usage with Visual Studio Diagnostic Tools, dotMemory, or PerfView to identify which object types are consuming the most memory and trace their allocation path.
  2. Ensure all IDisposable objects (streams, connections, readers) are wrapped in `using` statements to guarantee timely release.
  3. Audit event subscriptions — objects referenced only by event handlers will not be collected. Use weak references or ensure handlers are unsubscribed when the subscriber is no longer needed.
  4. If targeting 32-bit: recompile the application as 64-bit (`<PlatformTarget>x64</PlatformTarget>` or `AnyCPU`) to access a much larger address space.
  5. Reduce LOH allocations by pooling large objects using `ArrayPool<T>.Shared` or `MemoryPool<T>` instead of allocating fresh arrays, and configure LOH compaction when needed: `GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce`.
  6. Stream large data sources (files, HTTP responses) rather than buffering them fully into memory.
  7. Enable server GC mode in production (`<gcServer enabled="true"/>` in app.config) for better throughput and memory management under load.
OutOfMemoryException class — .NET
OutOfMemoryException in .NET: causes and fixes | Error Atlas