Why this matters
- Nearly every Java leak in practice comes from one of about six patterns, so recognising them is faster than any tool.
- Reading a heap dump is the one skill that turns "the service restarts every six hours" into a specific line of code.
- This is where garbage collection, collections and the reference types all meet actual production behaviour.
What a leak looks like
The signature in a GC log or in jstat is distinctive: old generation occupancy climbing steadily, never
returning to a baseline after a full collection, with full collections becoming more frequent and longer
until the application spends most of its time collecting and then fails.
The patterns that cause almost all of them
A static collection that only grows
A static field is a GC root. Everything it reaches is permanently live. This is the single most common
Java leak.
An unbounded cache
A cache without an eviction policy is a leak with a helpful name. Use a bounded LinkedHashMap with LRU
eviction, or a cache library with size and time limits.
A listener that is never unregistered
The publisher usually outlives the subscriber. Every registration not paired with a removal keeps the subscriber — and its entire reachable graph — alive.
A ThreadLocal in a pooled thread
ThreadLocal values are cleared when the thread dies. A thread-pool thread does not die, so the value
survives for the pool's lifetime, and the next request inherits stale data as well as leaking memory.
A mutable key in a hash map
This is a genuine leak as well as a lookup bug: the entry cannot be found, so it cannot be removed either.
An inner class outliving its enclosing object
Make it static and pass only what it needs. The same applies to a lambda that captures this.
| Aspect | Pattern | Fix |
|---|---|---|
| static collection that grows | A static field is a GC root | Bound the size, or do not keep it in memory |
| Unbounded cache | Every key retained forever | LRU eviction, or a cache library with limits |
| Unregistered listener | Publisher outlives subscriber | Pair every add with a remove |
| ThreadLocal in a pool | Pooled threads never die | remove() in a finally block |
| Mutable map key | Entry stranded in the wrong bucket | Immutable keys only |
| Non-static inner class | Implicit outer reference | Make it static; pass what it needs |
| Unclosed resources | Native memory and file handles | try-with-resources |
static collection that grows
PatternA static field is a GC rootFixBound the size, or do not keep it in memoryUnbounded cache
PatternEvery key retained foreverFixLRU eviction, or a cache library with limitsUnregistered listener
PatternPublisher outlives subscriberFixPair every add with a removeThreadLocal in a pool
PatternPooled threads never dieFixremove() in a finally blockMutable map key
PatternEntry stranded in the wrong bucketFixImmutable keys onlyNon-static inner class
PatternImplicit outer referenceFixMake it static; pass what it needsUnclosed resources
PatternNative memory and file handlesFixtry-with-resources
Six patterns and a resource check cover the overwhelming majority of real Java leaks.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 5 sections
- Capturing a heap dump
- Analysing it
- Live diagnosis
- Preventing them
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.