Java Memory Leak Detection — Heap Dump Analysis
Fast recall
Quick revision
Core answer
-XX:+HeapDumpOnOutOfMemoryError or jcmd GC.heap_dump). Open in Eclipse MAT — dominator tree and leak suspects point at retaining paths.Practical production debugging skills for memory issues
Increasing heap size as a fix — it delays OOM but doesn't fix the leak
Explain Eclipse MAT's 'Leak Suspects' report and the dominator tree for finding root cause
Interview-ready guide
Interview answer
Memory leaks in Java happen when objects that are no longer needed remain reachable through a chain of strong references to a GC root — the GC is doing its job correctly, but something is holding the reference.
Confirming the leak:
- Run
jstat -gcand watch the post-full-GC heap baseline - Baseline creeping upward over hours → leak; stays flat but GC runs constantly → heap undersized
Root cause analysis:
- Capture heap dump: on-demand with
jmap -dump:live,format=b,file=heap.hprofor via-XX:+HeapDumpOnOutOfMemoryError - Open in Eclipse MAT: run Leak Suspects report; Dominator Tree shows which objects retain the most memory
- Follow the reference chain from the suspect to its GC root — reveals exactly why the object can't be collected
Most common sources:
- Static collections that grow without bounds
ThreadLocalvalues not removed on thread-pool threads —try-finally remove()is mandatory- Listener/observer registries that subscribe but never unsubscribe
I recommend deploying with -XX:+HeapDumpOnOutOfMemoryError enabled and using Caffeine or Guava Cache — unbounded HashMap caches are the single most common leak pattern in production.
Java Memory Leak Detection — Heap Dump Analysis
Direct Answer
Detect leaks with rising old-gen after full GC, then capture a heap dump (-XX:+HeapDumpOnOutOfMemoryError or jcmd GC.heap_dump). Open in Eclipse MAT — dominator tree and leak suspects point at retaining paths.
Common Mistakes & Pitfalls
Increasing heap size as a fix — it delays OOM but doesn't fix the leak
Check your understanding
Answer these without looking back at the article.
- What is a WeakReference and when should you use it?
- How do you capture a heap dump without stopping the JVM?
- What is the difference between a memory leak and excessive memory allocation?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.