Quick revision
The short answer
I would first confirm it is a leak by watching heap usage after each full GC climb over time, then capture a heap dump, either automatically with -XX:+HeapDumpOnOutOfMemoryError writing to a mounted volume, or on demand with jcmd GC.heap_dump while the heap is already large. In Eclipse MAT I would use the dominator tree, retained sizes and paths to GC roots to find what is holding the memory, which is usually an unbounded cache or map, a ThreadLocal in a pooled thread, a listener that is never removed or oversized session data. The dump contains real data, so it must be handled under our data policies.
What to remember
In this answer, we saw that a Java leak is memory that stays reachable after the program stops needing it, and that the way to confirm one is a rising heap floor after GC over time. We capture a heap dump with -XX:+HeapDumpOnOutOfMemoryError on a mounted volume or with jcmd GC.heap_dump, and in MAT we use the dominator tree, retained sizes and paths to GC roots to find the field that holds the memory. The usual causes are unbounded caches, ThreadLocals in pooled threads, forgotten listeners and large sessions, and the dump itself must be handled as sensitive data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would first confirm it is a leak by watching heap usage after each full GC climb over time, then capture a heap dump, either automatically with -XX:+HeapDumpOnOutOfMemoryError writing to a mounted volume, or on demand with jcmd GC.heap_dump while the heap is already large. In Eclipse MAT I would use the dominator tree, retained sizes and paths to GC roots to find what is holding the memory, which is usually an unbounded cache or map, a ThreadLocal in a pooled thread, a listener that is never removed or oversized session data. The dump contains real data, so it must be handled under our data policies.
Why this question is asked
A slow leak is one of the most common production problems in Java services, and it is easy to mishandle. Interviewers ask this to see whether we know the difference between a leak and a busy heap, how to collect evidence safely, and how to read a heap dump. In this answer, we will recap what a leak means in a garbage-collected language, confirm the trend, capture a dump, analyse it step by step, and look at the usual causes and how to fix one of them.
1What a Leak Means in Java
Java has a garbage collector, so we never free memory by hand. The collector finds objects that can no longer be reached from a GC root, which is a starting point the collector always treats as live, such as a local variable in a running thread, a static field or a JNI reference. Everything reachable from a root stays; everything else is reclaimed.
So a Java leak is not forgotten memory. It is memory that is still reachable even though the program no longer needs it. A static map that collects an entry per request and never removes it is the classic example. Every entry is reachable from a static field, so the collector must keep it, and the heap grows a little with every request until the JVM throws OutOfMemoryError: Java heap space.
2Confirming the Trend With Heap After GC
A heap that is mostly full is not a leak by itself. The JVM lets garbage accumulate and cleans it up later, so used heap goes up and down like a saw. What matters is the floor of that saw: the amount still in use right after a full or mixed collection, which is the live data. If the floor rises steadily over two days, something is holding on to objects.
Notice that the slowdown has a cause too. As live data grows, each collection frees less, so the collector runs more often and spends more CPU. Requests slow down for a day or more before the final crash.
3Capturing a Heap Dump Safely
A heap dump is a file with every object in the heap and the references between them. We want one taken when the heap is large, because then the leaking objects dominate it. In a container, the path must be on a mounted volume, otherwise the dump disappears with the crashed container.
But there is a catch: taking a dump pauses the application for seconds, and the file is about as large as the used heap. So on a live service we take the pod out of the load balancer first, and we make sure the volume has room.
4Reading the Dump in Eclipse MAT
Eclipse Memory Analyzer (MAT) is a free tool built for this; Its most useful idea is retained size: the memory that would be freed if a given object were collected, including everything only it keeps alive. An object's shallow size is just its own fields, so a small HashMap object can have a shallow size of 48 bytes and a retained size of 1.2 GB.
The dominator tree lists objects by retained size. An object dominates another when every path from a GC root to the second passes through the first. So the top of the dominator tree is the set of objects actually holding the memory. Once we find a suspicious one, the path to GC roots view (excluding weak and soft references) shows the chain of references from a root to it.
- Check heap after GC over timeA rising floor confirms a leak
- Capture a heap dumpOn OOM to a volume, or jcmd while large
- Open the dump in MATRun the leak suspects report
- Sort the dominator tree by retained sizeFind the object holding most memory
- Show paths to GC rootsExclude weak and soft references
- Find the field and the code that fills itThen write the fix and a test
6 steps in order. Play them, or step through them one at a time.
Each step narrows the search from the whole heap to one line of code.
In our example, the top entry would be a ConcurrentHashMap with 1.2 GB retained, reached from a static field in a class called PriceCache.
5The Usual Causes and a Fix
Most leaks fall into a few patterns. An unbounded cache or map keeps every key it ever saw. A ThreadLocal set in a pooled thread, such as a Tomcat worker, stays attached to that thread forever if nobody calls remove(), because pooled threads never die. Listeners or callbacks registered on a long-lived object and never unregistered keep their owners alive. And large HTTP session data, kept for thousands of users until sessions expire, can fill the heap during busy periods.
Here is the leaking cache from our example and a fix with a bounded cache.
Notice that the real bug was the key: adding the customer id made almost every request a new entry. Bounding the cache stops the crash, and fixing the key restores the hit rate.
6Heap Dumps Contain Real Data
A heap dump is a copy of everything in memory: customer names, emails, tokens, passwords in request objects, and sometimes card data. It must be treated like a production database export. That means storing it in an access-controlled, encrypted location, sharing it only with the people investigating, never attaching it to a ticket or a chat message, and deleting it when the investigation ends, according to our data retention policy.
7Follow-up questions
How is a leak different from a heap that is simply too small?
With a heap that is too small, the live data after GC is high but flat: the service needs, say, 1.8 GB of live objects and we gave it 2 GB. Raising the heap fixes it. With a leak, the floor keeps rising no matter how big the heap is, and a bigger heap only delays the crash.
Can a leak happen outside the heap?
Yes. Direct ByteBuffers, Netty buffers, thread stacks, metaspace from class loaders that are never released, and native libraries all use memory outside the Java heap. Those show up as the container being OOMKilled while the heap looks healthy, and Native Memory Tracking (-XX:NativeMemoryTracking=summary with jcmd VM.native_memory) helps there.
8Conclusion
In this answer, we saw that a Java leak is memory that stays reachable after the program stops needing it, and that the way to confirm one is a rising heap floor after GC over time. We capture a heap dump with -XX:+HeapDumpOnOutOfMemoryError on a mounted volume or with jcmd GC.heap_dump, and in MAT we use the dominator tree, retained sizes and paths to GC roots to find the field that holds the memory. The usual causes are unbounded caches, ThreadLocals in pooled threads, forgotten listeners and large sessions, and the dump itself must be handled as sensitive data.
Open with
First I would confirm it is really a leak. I would look at heap usage after GC over the two days, from GC logs or a Micrometer metric.
Then, if they want more
If the floor keeps climbing, something is holding on to objects, and the slowdown is the GC working harder as free space shrinks. Next I need a heap dump.
I would make sure the service runs with HeapDumpOnOutOfMemoryError and a HeapDumpPath on a mounted volume, so the next crash leaves a dump behind, and I can also take one with jcmd GC.heap_dump while the heap is large, after taking the pod out of traffic because it pauses the JVM. Then I open it in Eclipse MAT.
I look at the dominator tree sorted by retained size, which shows the objects that keep the most memory alive, and use path to GC roots to see which field and class hold them.
In my experience it is usually an unbounded cache or static map, a ThreadLocal that is never removed in a pooled thread, listeners that are never unregistered, or large session data.
The fix is to bound or remove that reference, for example replacing a static map with a Caffeine cache with a maximum size, and then I watch the heap after GC to confirm the floor is flat.
I would also treat the dump as sensitive, because it contains customer data, so it stays in a restricted location and is deleted afterwards.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation, Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Java service gradually slows down over two days and then crashes with...": https://scrivio-interview-library.netlify.app/spring-boot/production-operations/9954a489e0/