Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Why .NET GC Doesn't Prevent Memory Leaks: 5 Common Causes

This article debunks the myth that .NET's garbage collector eliminates memory leaks, explaining five common leak scenarios — event subscription leaks, static collection growth, unmanaged resource leaks, large object heap fragmentation, and thread pool task references — and outlines a systematic diagnostic process using dotnet-counters, dotnet-gcdump, and dotnet-dump.

The Misconception: GC Does Not Guarantee No Memory Leaks

A common interview question asks whether .NET can have memory leaks given its garbage collector. Many candidates incorrectly answer that GC automatically prevents leaks. This is a dangerous misunderstanding. In .NET, memory leaks typically occur not because objects are uncollectible, but because they are unintentionally kept reachable . The GC only collects unreachable objects; if an object remains referenced, it will never be collected regardless of whether it is still needed.

Five Common .NET Memory Leak Scenarios

1. Event Subscriptions Not Unsubscribed

When a subscriber registers for an event but never unsubscribes, the publisher holds a reference to the subscriber. As long as the publisher lives, the subscriber cannot be collected. The longer the publisher's lifetime, the longer the subscriber is retained.

2. Static Collections Growing Indefinitely

A static List<T> that continuously adds objects without removal causes a leak. Static fields are GC roots, so every object they reference remains reachable forever and is never collected.

3. Unmanaged Resources Not Released

File handles, database connections, sockets, and other unmanaged resources are not managed memory. The GC does not track or release them. Failure to explicitly dispose of these resources leads to leaks outside the managed heap.

4. Large Object Heap (LOH) Fragmentation

Objects larger than 85 KB are allocated on the Large Object Heap. By default, the LOH is not compacted. Frequent creation of large objects causes memory fragmentation; even when total free memory is sufficient, a contiguous block may be unavailable, triggering out-of-memory conditions.

5. Thread Pool Task References

Long-running tasks in the thread pool can hold references to objects, unexpectedly extending those objects' lifetimes and preventing collection.

Systematic Diagnostic Process for Memory Leaks

Troubleshooting memory leaks is not guesswork; it follows a structured diagnostic flow:

Signal identification: Look for sustained growth in the managed heap size (not just Task Manager memory fluctuations).

Toolchain: Use dotnet-counters to monitor GC metrics, dotnet-gcdump to capture heap snapshots, and dotnet-dump for deeper analysis.

Root cause analysis: Employ the !gcroot command (in SOS or dotnet-dump) to trace the reference chain keeping the leaked object alive.

When production memory keeps rising, the first step is to capture metrics with dotnet-counters and then proceed through the toolchain to isolate the leak source.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Garbage CollectionThread PoolDiagnosticsMemory Leaks.NETEvent Subscriptiondotnet-countersdotnet-dumpdotnet-gcdumpLarge Object HeapStatic CollectionsUnmanaged Resources

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion