Skip to main content
PreparationZone Logo
PrepZone

Finding Memory Leaks

The handful of patterns that cause nearly every Java leak, and how to confirm one from a heap dump.

Pass checkpoint below

Read these first

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

Static collectionGrows forever, never cleared
Unclosed resourceStreams and connections held open
Listener not removedPublisher keeps the subscriber alive
ThreadLocal in a poolPooled thread never dies, value stays
A Java leak is never forgotten free(); it is always an object still reachable from something long-lived. Find the owner and the leak explains itself.

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

Java
Loading compiler…

A static field is a GC root. Everything it reaches is permanently live. This is the single most common Java leak.

Java
Loading compiler…

An unbounded cache

Java
Loading compiler…

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.

Java
Loading compiler…

A listener that is never unregistered

Java
Loading compiler…

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

Java
Loading compiler…

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.

Java
Loading compiler…

A mutable key in a hash map

Java
Loading compiler…

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

Java
Loading compiler…

Make it static and pass only what it needs. The same applies to a lambda that captures this.

AspectPatternFix
static collection that growsA static field is a GC rootBound the size, or do not keep it in memory
Unbounded cacheEvery key retained foreverLRU eviction, or a cache library with limits
Unregistered listenerPublisher outlives subscriberPair every add with a remove
ThreadLocal in a poolPooled threads never dieremove() in a finally block
Mutable map keyEntry stranded in the wrong bucketImmutable keys only
Non-static inner classImplicit outer referenceMake it static; pass what it needs
Unclosed resourcesNative memory and file handlestry-with-resources
  • static collection that grows

    PatternA static field is a GC root
    FixBound the size, or do not keep it in memory
  • Unbounded cache

    PatternEvery key retained forever
    FixLRU eviction, or a cache library with limits
  • Unregistered listener

    PatternPublisher outlives subscriber
    FixPair every add with a remove
  • ThreadLocal in a pool

    PatternPooled threads never die
    Fixremove() in a finally block
  • Mutable map key

    PatternEntry stranded in the wrong bucket
    FixImmutable keys only
  • Non-static inner class

    PatternImplicit outer reference
    FixMake it static; pass what it needs
  • Unclosed resources

    PatternNative memory and file handles
    Fixtry-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.