Troubleshooting guide
.NET OutOfMemoryException: causes and how to fix it
The .NET runtime could not allocate the memory requested. That may be a leak, a container limit or one allocation too big for any contiguous block.
3 min read10 sectionsUpdated
On this page
# Install once
dotnet tool install -g dotnet-counters
dotnet tool install -g dotnet-gcdump
# Live GC and heap counters
dotnet-counters monitor -p PID System.Runtime
# Snapshot object counts and sizes
dotnet-gcdump collect -p PID
dotnet-gcdump report ./*.gcdump | head -30
# Full process dump for deeper analysis
dotnet-dump collect -p PIDFixes
Unsubscribe from events, or use weak references. Add size limits and expiry to MemoryCache. Stream with IAsyncEnumerable. Reuse buffers with ArrayPool<T> to avoid LOH churn. In containers, tune DOTNET_GCHeapHardLimitPercent if the default does not fit. Consider Server GC for throughput, or Workstation GC for small containers. Enable DOTNET_gcServer deliberately, because it changes how much memory the runtime reserves on many-core hosts.
Comparing snapshots
As with other runtimes, one snapshot says little: collect two some time apart, and look for types whose counts keep going up. In Visual Studio or PerfView, you can diff two .gcdump files directly. Follow the paths to root for a growing type to see what's holding it. Commonly it is a singleton service with a captured scoped dependency: registering something as a singleton that holds per-request data is a frequent ASP.NET Core bug. IHttpClientFactory also matters: a new HttpClient per request leaks handlers and sockets.
Catch it automatically
Chart GC heap size, gen2 collections and LOH size in .NET performance monitoring. A rising baseline after each gen2 collection indicates a leak.
What is the Large Object Heap?
A separate heap for objects of 85,000 bytes or more, which is compacted only on request.
Can I catch OutOfMemoryException?
You can, but state may be unreliable.
Does .NET respect container memory limits?
Yes, modern versions do, defaulting the heap limit to 75 percent.
Related reading
- Elasticsearch CircuitBreakingException: how to fix it: Fix Elasticsearch CircuitBreakingException Data too large: reduce fielddata and huge aggregations, size JVM heap correctly and check shard counts per node.
- How to find a Node.js memory leak: Detect a Node.js memory leak from heap metrics, compare heap snapshots to find what retains memory, fix the usual causes and alert before a crash.
- 414 URI Too Long: causes and how to fix it: Fix 414 URI Too Long errors: redirect loops that grow the URL, huge query strings and large GET filters, with Nginx and Apache settings and better designs.
- Goroutine leaks in Go: how to find and fix them: Find and fix goroutine leaks in Go: read pprof goroutine profiles, spot blocked channels and missing context cancellation, and use goleak in your tests.
- Java memory usage: heap, metaspace and leaks: Understand Java memory usage: heap, metaspace, threads and direct memory, why RSS exceeds -Xmx, how to size for containers.
- JavaScript heap out of memory: how to fix it in Node.js: Fix FATAL ERROR: JavaScript heap out of memory. Set --max-old-space-size correctly, handle build-time failures and find runtime leaks with heap snapshots.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.