When your Java application starts misbehaving – hanging, crashing, or consuming too much memory – thread and heap dumps are your go-to tools for root cause analysis. This post walks through how to create these dumps using the jstack
, jmap
, and jcmd
tools, and suggests utilities for analyzing them effectively. Use jps
to identify the process ID you’re interested in.
๐งต Thread Dumps: Capturing Thread States
1. Using jstack
jstack <PID> > threaddump.txt
- Produces a snapshot of all thread states (blocked, runnable, waiting).
- Great for diagnosing deadlocks, thread contention, and CPU spikes.
2. Using jcmd
jcmd <PID> Thread.print > threaddump.txt
- More modern and flexible than
jstack
. - Lists thread locks and monitors.
๐ง Heap Dumps: Capturing Memory Snapshots
1. Using jmap
jmap -dump:live,format=b,file=heapdump.hprof <PID>
- Dumps the live heap memory (excluding unreachable objects).
- Useful for analyzing memory leaks and object retention.
2. Using jcmd
jcmd <PID> GC.heap_dump heapdump.hprof
- Recommended over
jmap
for modern JVMs (especially with limited access). - Can work even when
jmap
fails due to permissions or restrictions.
๐งฐ Tools to Analyze Dumps
๐ Thread Dump Analysis
- jstack.review – Analyze java thread dumps from within the browser. No data will leave your computer when you click Analyze.
- FastThread.io โ Upload your dump and get a categorized summary.
- TDA (Thread Dump Analyzer) โ Desktop tool for visualizing thread interactions.
๐ง Heap Dump Analysis
- Eclipse MAT (Memory Analyzer Tool) โ Industry-standard tool for analyzing
.hprof
files. - VisualVM โ Bundled with the JDK or available separately, it provides a GUI for live analysis and offline dump inspection.
- YourKit โ Commercial profiler with advanced heap analysis and leak detection.
๐ Final Thoughts
Thread and heap dumps are crucial artifacts for JVM troubleshooting. While tools like jstack
and jmap
provide raw diagnostic data, leveraging jcmd
offers greater flexibility and reliability on modern JVMs. Combined with powerful analysis tools, these techniques can help you uncover performance bottlenecks and memory issues before they escalate into outages.
Happy debugging! ๐
Leave a Reply