dotnet runtime_exception ai_generated true

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

ID: dotnet/outofmemoryexception

Also available as: JSON · Markdown
78%Fix Rate
82%Confidence
65Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8 active

Root Cause

OutOfMemoryException occurs when the CLR cannot allocate enough memory. Common causes include loading large datasets into memory, memory leaks from undisposed resources, and Large Object Heap fragmentation. Fix by streaming data, disposing resources, and profiling memory usage.

generic

Workarounds

  1. 85% success Stream large data instead of loading it entirely into memory
    Replace patterns like File.ReadAllBytes() or ToList() on large queries with streaming alternatives: use IAsyncEnumerable, StreamReader, or EF Core's AsAsyncEnumerable() to process data in chunks rather than loading everything at once
  2. 82% success Profile memory usage and fix leaks using dotnet-dump and dotnet-gcdump
    Run 'dotnet-gcdump collect -p <pid>' to capture a GC heap snapshot. Analyze with Visual Studio or PerfView to find types with unexpectedly high instance counts. Common leak sources: event handlers not unsubscribed, static collections growing unbounded, HttpClient instances not reused via IHttpClientFactory

Dead Ends

Common approaches that don't work:

  1. Simply increasing the container or VM memory limit without investigating the root cause 80% fail

    Memory leaks and unbounded allocations will eventually consume any amount of memory; increasing limits only delays the inevitable crash and increases infrastructure costs

  2. Calling GC.Collect() frequently to force garbage collection 75% fail

    Forced GC causes performance degradation due to stop-the-world pauses; if memory is still referenced (leak), GC cannot reclaim it regardless of how often it runs

Error Chain

Leads to:
Preceded by:
Frequently confused with: