JVM tool guide · jvm cli

jstack: Capture Thread Dumps for Hang & Deadlock Analysis

jstack prints the Java stack traces of all threads in a running JVM. It is the essential tool for hang and deadlock investigations: when a service stops responding, a thread dump shows exactly where every thread is blocked, waiting on a lock, or spinning.

The modern equivalent via jcmd is Thread.print; whichever you use, the investigation technique — take two or three dumps a few seconds apart and diff them — is what matters most.

Official jstack project

Use it when

• A service looks hung — take a thread dump and see what each thread is doing.

• You suspect a deadlock: jstack prints 'Found one Java-level deadlock' when it detects one.

• You need to know if threads are blocked on a monitor or waiting on a condition (matched with the -l option).

Skip it when

• You need heap layout (use jmap or jcmd GC.heap_dump).

• You need ongoing profiling rather than a snapshot (use JFR/async-profiler).

Dump all threads

Print the stack of every thread. Redirect to a file for repeated dumps you can diff.

# Full dump to stdout
jstack <pid>

# With lock info, to a file
jstack -l <pid> > threads-$(date +%s).txt

# Same thing via jcmd
jcmd <pid> Thread.print -l

Detect deadlocks

jstack automatically scans for cyclic lock waits and reports the implicated threads in its output.

jstack <pid> | grep -A 10 -i 'deadlock'

Recommended hang-investigation cadence

Take dumps a few seconds apart. If the same threads are stuck in the same frames across dumps, it is almost certainly a hang, not a transient wait.

for i in 1 2 3; do
  jstack <pid> > threads-$i.txt
  sleep 5
done
diff <(cut -c1-120 threads-1.txt) <(cut -c1-120 threads-2.txt)

What a hung thread looks like

Look for threads in RUNNABLE spinning in the same method, or WAITING/blocked on a monitor that is never released. HEAD of the stack is where the thread is now; the 'at' frames below show the call path.

Quick start

Get productive in minutes

Investigate a hang in 30 seconds

Two dumps + a diff.

jstack <pid> > t1.txt
sleep 5
jstack <pid> > t2.txt
diff t1.txt t2.txt

Frequently asked questions

Hold on, do I use jstack or jcmd Thread.print?

Both produce thread dumps. jcmd Thread.print -l is the modern form and plays nicely with other jcmd commands. jstack remains fine and is heavily used in existing runbooks.

Why take two dumps for a hang?

A single dump can catch a thread in a momentary wait. Two dumps seconds apart that agree are strong evidence the threads are genuinely stuck, not just momentarily paused.

Can I read the dump without line numbers?

Running jstack -l and using a tool like TDA (Thread Dump Analyzer) or the thread-dump views in JMC/VisualVM makes large dumps far easier to scan.

Last updated August 2026 · JVM Tools is independent and not affiliated with Oracle.