Skip to content

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

Key points

  • dotnet-counters gives a live view of the heap.
  • The Large Object Heap holds objects of 85,000 bytes or more.
  • Containers set the GC heap limit automatically to 75 percent.
  • Static events and caches are the usual leaks.

What it means

Allocation failed after the GC tried to free memory. It does not always mean all memory is used: a single large array needs contiguous space, and a fragmented Large Object Heap may not have it.

Common causes

  • Event handlers keeping subscribers alive.
  • Unbounded static caches.
  • Large strings or byte arrays built repeatedly.
  • Reading whole files or query results.
  • Container limits smaller than the workload.

Diagnose it

Use the .NET diagnostic tools, which work without restarting the process.

bash
# 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 PID

Fixes

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.

Frequently asked questions

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.

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.