heapdump
Heapdump: The Complete Guide to Memory Dumps for Java, Python, and Node.js Developers
A heapdump is a point-in-time snapshot of an application's memory heap — a forensic artifact that reveals every allocated object, every reference chain, and every byte of wasted space. For engineers debugging OutOfMemoryError, chasing memory leaks, or sizing production workloads, the heapdump is not just useful; it is often the only way to see the problem clearly. This guide walks through what heapdumps are, how to capture them across languages, how to analyze them, and how to turn raw binary into actionable fixes.
Key Takeaways
- A heapdump captures the Java object graph at a single instant, including all live objects, their sizes, and reference relationships.
- Common tools: jmap (JDK), Eclipse MAT, VisualVM, YourKit, JProfiler, and jdummer for quick CLI inspection.
- Capture methods vary by scenario: on OOM auto-dump via
-XX:+HeapDumpOnOutOfMemoryError, manual on-demand via jcmd, or post-mortem from a core dump. - Heap size alone is not the answer — most leaks stem from unbounded collections, cached entries without eviction, or classloader retention.
- Always compress and sanitize heapdumps before sharing — they contain serialized application data and may include sensitive values.
What Is a Heapdump?
Definition and Purpose
A heapdump (also written heap dump) is a serialized representation of the garbage-collected heap of a running process. In JVM-based languages (Java, Kotlin, Scala, Groovy), the dump contains every live object reachable from GC roots, along with class metadata, allocation call stacks, and retained sizes. The file format is vendor-specific but widely supported — HPROF is the default JDK format, while Eclipse MAT can also read .phd (PerfDash) and .hprof files.
The primary purposes of a heapdump are:
- Memory leak detection — identify objects that should have been collected but are still retained.
- OutOfMemoryError root-cause analysis — determine what consumed the heap before the crash.
- Allocation profiling — understand which code paths generate the most garbage.
- Capacity planning — measure actual vs. configured heap usage in production.
Heapdump vs. Thread Dump vs. Core Dump
These three artifacts are often confused. Here is how they differ:
| Artifact | What It Captures | Typical Size | Primary Use Case | Best Tool |
|---|---|---|---|---|
| Heapdump | All live objects + references on the GC heap | 100 MB – 10+ GB | Memory leaks, OOM analysis, allocation hotspots | Eclipse MAT, VisualVM |
| Thread dump | Stack traces of every thread at one instant | 50 KB – 5 MB | Deadlocks, thread starvation, CPU spikes | tda, fastthread.io |
| Core dump | Entire process memory (heap, stack, BSS, mmap) | Equal to RAM usage | Crash debugging, native OOM, SIGSEGV | GDB, lldb |
How to Capture a Heapdump
Java / JVM Applications
Automatic Dump on OOM
The most reliable production strategy is to let the JVM dump the heap automatically when an OutOfMemoryError occurs:
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/dumps/heap.hprof
-XX:OnOutOfMemoryError='kill -QUIT %p'
This configuration writes a full HPROF file the moment the GC cannot reclaim space. The OnOutOfMemoryError hook sends a QUIT signal, which triggers a concurrent thread dump alongside the heapdump.
Manual On-Demand Dump
Use jcmd (available from JDK 7u40+) to request a heapdump from a running process without restarting it:
jcmd <pid> GC.heap_dump /tmp/heap-$(date +%s).hprof
This is the preferred production method because it is non-invasive and works against any HotSpot-compatible JVM.
Legacy jmap Method
Before jcmd, jmap was the standard tool:
jmap -dump:live,format=b,file=heap.hprof <pid>
The live flag forces a garbage collection before dumping, so only surviving (live) objects are included — typically reducing file size by 30–60%.
Python Heap Dumps
Python does not have a built-in heapdump command, but several tools provide equivalent functionality:
- tracemalloc (stdlib) — tracks memory allocations with call-site precision.
- muppy — finds unreachable objects and reference cycles in CPython.
- objgraph — visualizes object reference graphs and common leak patterns.
- pickle + gc.get_objects() — custom approach to serialize the entire object graph.
Node.js Heap Snapshots
Node.js includes a built-in V8 heap snapshot mechanism:
const { Snapshot } = require('inspector');
Snapshot.take().saveAsync('heap.snappoint');
// Or via CLI: node --inspect-brk app.js, then use Chrome DevTools → Memory → Take Heap Snapshot
V8 heap snapshots (.heapsnapshot) are JSON-based and can be opened directly in Chrome DevTools or Google Chrome's chrome://inspect memory profiler.
Analyzing a Heapdump
Step-by-Step Workflow
- Load the dump into your analyzer of choice. Eclipse MAT handles multi-gigabyte files better than most.
- Check the histogram — view object counts and shallow sizes by class. Look for anomalously large buckets.
- Run the leak suspect report — Eclipse MAT computes dominator trees and identifies objects retaining the most heap.
- Inspect the oql query results — use Object Query Language to filter specific classes or reference chains.
- Trace the GC root — follow the shortest strong reference path from a retained object back to its GC root.
- Correlate with code — map the retained object to its allocation site (if available) or the nearest stack frame.
Common Leak Patterns
| Pattern | Description | Frequency | Fix |
|---|---|---|---|
| Static collection growth | A static Map or List accumulates entries without eviction |
Very common | Use CacheBuilder (Guava) or LruCache; set size bounds |
| Unclosed resources | Streams, connections, or cursors never closed, holding heap and native memory | Common | Use try-with-resources; implement AutoCloseable |
| Listener / callback retention | Observers registered |