How to Capture and Read Heap Dumps and Thread Dumps
The jcmd, jmap and Node commands that capture each dump, how to read states, histograms and snapshots, and the leak signatures that settle most incidents.
By Redbison Dev · · Updated · 10 min read

Your Java service has stopped answering, or its memory graph is a staircase that only goes up, and someone needs an answer before the restart erases the evidence. The move that separates a fixable incident from a mystery is capturing the right dump first: a thread dump if the process is stuck or slow, a heap dump if memory is the problem, a heap snapshot if the process is Node or a browser tab. This guide covers the exact commands that capture each one, how to read what comes out, and the leak signatures that settle most cases, using the Java Thread Dump Analyzer, Java Heap Dump Analyzer and Heap Snapshot Analyzer, which parse everything in your browser so the dump never leaves your machine.
Capture the evidence before you restart
A dump taken one minute before the restart is worth more than a week of guessing afterwards. The commands, in the order you will need them (every method and option, measured on JDK 17, is in the separate guides to taking a Java heap dump and a Java thread dump):
Thread dump (JVM). jcmd <pid> Thread.print prints every thread with its state, stack and lock annotations; add -l to include java.util.concurrent lock details. jstack <pid> prints the same format but its own JDK manual page marks it experimental and unsupported, so prefer jcmd. On a container where you only have the console log, kill -3 <pid> makes the JVM print the dump to stdout without stopping. A thread dump costs milliseconds; take three, 5 to 10 seconds apart, so you can tell a stuck thread from a busy one.
Heap dump (JVM). jcmd <pid> GC.heap_dump /tmp/app.hprof writes the HPROF binary file. By default it requests a full GC first and dumps live objects, which is what you want for a leak hunt; -all keeps unreachable garbage too, and -gz=1 compresses while writing (the = is required: -gz 1 fails with a parse error on JDK 17). The older equivalent is jmap -dump:live,format=b,file=app.hprof <pid>. Most valuable of all is -XX:+HeapDumpOnOutOfMemoryError, set before the incident, which writes the dump at the exact moment the heap fills. Be aware the JVM pauses while writing: seconds for small heaps, minutes for very large ones, so on a sick node dump just before the restart you were going to do anyway.
Heap snapshot (Node and Chrome). In Chrome: DevTools, Memory panel, Heap snapshot, Take snapshot, then save the .heapsnapshot file. For a Node process, start it with --heapsnapshot-signal=SIGUSR2 and kill -USR2 <pid> writes a Heap.*.heapsnapshot file on demand, or call v8.writeHeapSnapshot() in code. Every capture route, with measured pauses and memory, is in how to take a Node.js heap snapshot. The Node documentation warns that writing a snapshot is synchronous, blocks the event loop and needs memory around twice the heap at that moment, so on a process already near its limit use --heapsnapshot-near-heap-limit=3, which writes snapshots automatically as the heap approaches the ceiling instead of you signalling it at the worst time.
Reading a thread dump: verdict first
Drop a dump into the Java Thread Dump Analyzer (the Load a sample button shows a real one). For the 25-thread example dump in our test suite, an OpenJDK 17 process with a planted bug, it reports: 18 Java threads (11 daemon) plus 7 VM-internal threads, states 9 RUNNABLE, 6 TIMED_WAITING, 2 BLOCKED, 1 WAITING, and above all a red verdict: deadlock-a and deadlock-b wait on each other in a cycle.
The states carry most of the meaning, and the two that get confused are defined precisely in the java.lang.Thread.State javadoc. BLOCKED means the thread wants to enter a synchronized block whose monitor another thread holds; it can do nothing until the holder releases. WAITING means the thread called Object.wait(), Thread.join() or LockSupport.park() and is waiting to be signalled; it holds no claim on a monitor. Only BLOCKED threads can deadlock on monitors. RUNNABLE needs one caveat the javadoc states itself: a runnable thread may be waiting for operating system resources, which is why a thread stuck in a blocking socket read shows as RUNNABLE with a stack full of native frames.
A deadlock is a cycle in the waits-for graph: thread A holds monitor 1 and waits for monitor 2, thread B holds monitor 2 and waits for monitor 1. The JVM prints its own "Found one Java-level deadlock" section at the end of most dumps, but dumps that travel through log pipelines often lose it, so the analyzer rebuilds the graph from the - locked and - waiting to lock annotations and finds the cycle either way.
One trap to know: a thread inside Object.wait() still shows the monitor it entered as - locked in its stack, but wait() released that monitor. Read naively, the JDK's own Finalizer thread looks like a one-thread deadlock in every dump. It never is, and an analyzer has to model the release; ours has a regression test for exactly this case.
Beyond deadlocks, two patterns answer most "why is it slow" questions. Threads grouped by a shared name prefix (pool-worker-0 through 3, http-nio-8080-exec-N) are pools, and a pool whose members all sit at the same frames, 4 threads TIMED_WAITING in Thread.sleep in the example, is idle, starved or stuck on the same downstream. And the cpu= attribute (JDK 11 and later) is cumulative CPU since each thread started, so take two dumps 10 seconds apart and diff them with the Online Diff Tool: the thread whose cpu= grew by about 10,000 ms used a full core between the dumps.
Reading a heap dump: histogram, biggest objects, duplicates
An .hprof file is binary HPROF, the format described at the top of heapDumper.cpp in the OpenJDK sources, and it is usually as large as the heap it captured. The Java Heap Dump Analyzer streams the file through in 8 MiB slices, keeping only running totals, so a 4 GB dump summarises in an ordinary browser tab without being loaded whole. In our test suite, a dump assembled byte by byte from that format description, two com.acme.Cache instances and four int[64] arrays, reads back as exactly 6 objects and 1.0 KiB shallow, with int[] on top of the histogram at 4 instances and 1.0 KiB; the parser returns the same figures fed the file whole or one byte at a time.
The word shallow matters. The dump records data bytes: field values for instances, length times element size for arrays, with no per-object header. Retained size, what an object keeps alive through its references, needs a dominator tree over the whole graph, and a histogram tool that does not build one should say so rather than print numbers that look like Eclipse MAT's. The histogram still answers the first question: which class is big, and is it count (millions of small objects) or size (a few huge arrays).
The classic signatures, in the order they turn up in real dumps:
- A collection that only grows. Millions of HashMap$Node or ConcurrentHashMap$Node instances mean some map is used as a cache with no eviction. The fix is a bounded cache; the histogram tells you how many entries you are paying for.
- One enormous array. A single Object[] of hundreds of MB in the biggest-objects table is usually an ArrayList that something kept adding to, or a result set read fully into memory.
- Duplicate primitive arrays. The analyzer hashes the content of every primitive array of 64 bytes or more; in the test fixture, 3 identical int[64] arrays show as 512 B spent on repeat content, every byte beyond the first copy. At production scale this finds the same JSON blob cached per user and buffers cloned per connection, and it is often the cheapest win in the whole dump.
- byte[] and char[] on top. Normal in every dump; they back strings and buffers. Look at who owns them before blaming the arrays themselves.
Node and the browser: the heap snapshot
A .heapsnapshot is self-describing JSON: its snapshot.meta section names the fields each node row carries, which is why files from old Chrome versions and current Node both parse. The Heap Snapshot Analyzer reads that layout and reports node types, constructors by shallow bytes (V8's self_size, the same figure the DevTools shallow column shows), duplicate strings with their wasted bytes, and detached DOM nodes.
Detached nodes are the browser leak signature: elements removed from the document that JavaScript still references through a listener, closure or cache. V8 marks them in the snapshot, DevTools filters them under "Detached", and a count that grows as you use the page means views are being removed but never released. In Node, the equivalent tell is a constructor count that only rises between two snapshots: Socket, IncomingMessage, or one of your own classes. Always compare two snapshots minutes apart; a single snapshot shows you a cache, two show you a leak.
When the browser summary is enough
The three analyzers answer what is big, what is duplicated, what is deadlocked and what is detached, in seconds, with the dump never leaving your machine. That last property is not a convenience: a heap dump or snapshot contains every live string in the process, session tokens and customer data included, and in many shops such a file may not be uploaded anywhere. Treat the file itself as a secret.
Open the heavyweight tools when the question changes from "what" to "who owns it": Eclipse MAT or VisualVM for retained sizes, dominator trees and paths to GC roots in an .hprof; the Chrome DevTools Memory panel for retained sizes and the full Detached view in a snapshot. By then the histogram has already told you which class to chase, so the expensive tool opens with a hypothesis instead of a blank page. And once the incident is closed, write down what the dump showed while it is fresh; the incident postmortem guide covers how to turn it into a timeline people trust.
Tools for this job
- Java Thread Dump Analyzer: paste jstack or jcmd output; deadlocks, states, contention, pools and CPU, verdict first.
- Java Heap Dump Analyzer: drop an .hprof of any size; class histogram, biggest objects and duplicate-array waste, streamed with progress.
- Heap Snapshot Analyzer: drop a .heapsnapshot from Node or Chrome; constructors, duplicate strings and detached DOM nodes.
- Java GC Log Analyzer: a heap problem usually shows in the GC log first; pause percentiles, throughput and the heap trend over time.
- Online Diff Tool: diff the text summaries of two dumps taken minutes apart; the rows that changed are the story. The text diff guide shows how to compare noisy outputs without drowning.
Frequently asked questions
Which should I capture first, a thread dump or a heap dump?
Thread dump first, always: it costs milliseconds and nothing pauses. If memory is the complaint, follow with a heap dump, accepting its stop-the-world pause. The order matters because the heap dump's full GC can unblock a stalled process and destroy the thread-level evidence.
Can I analyze dumps from a machine I cannot copy files off?
Yes, that is the design: open the analyzer page on that machine and drop the file; parsing is local JavaScript and no byte of the dump is transmitted. The pages themselves are static and load from cache after the first visit.
How large will my heap dump file be?
Roughly the size of the used heap: a 6 GB heap writes an .hprof of about that size, since the format stores object data without headers. jcmd GC.heap_dump -gz=1 compresses while writing, by an amount that depends on the content: measured on JDK 17, about half the plain size for varied data and a fifteenth for a cache of zeroes. Decompress before analysis.
Do these dumps contain secrets?
Yes. A heap dump or snapshot includes every live string: tokens, cookies, request bodies, personal data. A thread dump is milder but still leaks internal class and host names. Store them like credentials, redact before attaching to tickets, and prefer analysis that never uploads them.
Sources and further reading
- java.lang.Thread.State (Java SE 21 API), Oracle. The six thread states and the BLOCKED versus WAITING distinction.
- The jcmd Command, Oracle. Thread.print and GC.heap_dump with their options.
- The jmap Command, Oracle. The -dump and -histo options and their live suboption.
- heapDumper.cpp, OpenJDK. The HPROF binary format description this guide's figures rest on.
- Node.js command-line API, OpenJS Foundation. The --heapsnapshot-signal and --heapsnapshot-near-heap-limit flags.
- Node.js V8 module, OpenJS Foundation. v8.writeHeapSnapshot(), its default file name and its memory and blocking caveats.
- Memory terminology, Chrome for Developers. Shallow versus retained size.
- Record heap snapshots, Chrome for Developers. Taking snapshots and the Detached filter.
Open the Java Heap Dump Analyzer
More Debugging
Every tool in the Debugging section, free and in your browser.