Here is an imaginary support ticket. A Java service handling order lookups for a logistics company runs perfectly after each Monday deployment. By Thursday its response times creep up. By Saturday it is restarted by the platform after an out-of-memory error. Someone restarts it on Monday and the cycle repeats. The team says, “But Java has a garbage collector. How can it leak?”

Keep that service in mind. We will come back to it.

What a garbage collector actually decides

A garbage collector frees objects that are unreachable: no chain of references leads to them from the roots, which are things like local variables on active threads, static fields and loaded classes. It cannot know whether you still need an object. If something reachable holds a reference to it, the object stays, even if no code will ever touch it again.

That is the entire explanation. A leak in a managed language is not memory that the collector lost track of. It is memory that your program is still, accidentally, pointing at.

What the symptoms look like

On a graph of heap usage after each full collection, a healthy service shows a sawtooth that returns to roughly the same floor. A leaking one shows a floor that rises steadily. Between those collections, the collector works harder and harder as free space shrinks, so CPU spent on garbage collection climbs and pauses lengthen; this is why our imaginary service slowed down for days before it crashed. In the end the runtime throws an out-of-memory error (in Java, OutOfMemoryError). Python and JavaScript runtimes behave similarly with their own messages.

The usual culprits

When the team finally takes a heap snapshot, they find a single static map holding 2.4 million entries. A developer added it as a quick cache of shipment lookups, keyed by tracking number. Nothing ever removes an entry. That is the most common leak in practice: an unbounded cache or collection that only grows.

Other recurring causes:

  • Listeners and callbacks registered on a long-lived object and never unregistered. The long-lived publisher keeps every subscriber alive. This is a classic in desktop UIs and in front-end code: a component adds an event handler to window and forgets to remove it when it disappears.
  • Thread-local values on pooled threads. A web server reuses threads; a value stored in a thread-local variable and never cleared lives as long as the thread, which means forever, and may also pin the class loader that loaded your application during redeploys.
  • Closures capturing more than expected. A small callback may hold a reference to an enclosing scope containing a large array.
  • Sessions or per-user objects with no expiry, so that each visitor leaves something behind.
  • Unclosed resources such as connections, streams or file handles. These leak operating-system resources rather than heap, but the symptom of slow decay is similar, and the garbage collector does not rescue you.

Finding it

Guessing from code reading is slow. The reliable method is to look at what is in memory.

  1. Confirm it is a leak and not simply a heap that is too small: watch memory after full collections over hours or days, not just the raw usage.
  2. Take a heap dump or snapshot when memory is high. For Java, tools such as jmap, VisualVM or Eclipse Memory Analyzer can do this; Chrome DevTools has heap snapshots for JavaScript; Python has tracemalloc in its standard library.
  3. Better still, take two snapshots some time apart and compare them. The object type whose count keeps growing is your suspect.
  4. Follow the path from the roots to a leaked object. Analyzers show a dominator tree, which tells you which single reference is keeping the most memory alive. In our story, it points at the static map.

Fixing, and preventing

The team replaces the raw map with a cache that has a maximum size and an expiry time (libraries like Caffeine in Java exist for exactly this). After the next deployment, the post-collection floor stays flat. The fix is a few lines; finding it was the real work.

For prevention: treat any long-lived collection as suspicious by default and ask what removes things from it. Give caches a bound. Unregister what you register, ideally in the same structure that created it. Use language constructs that scope resources, such as try-with-resources in Java, using in C#, or context managers in Python. Weak references can help for caches that should not by themselves keep objects alive, but they are a specialised tool and not a substitute for a clear ownership design.

And add monitoring before you need it: heap after collection, GC time, and object counts for your big collections. A trend that rises over days is visible long before the crash, if someone is watching the right graph.

Figures and names in the examples are illustrative.

Leave a Reply

Trending