jstack
Dump Java thread stacks for analysis
TLDR
Print Java stack traces for all threads in a Java process
Print mixed mode (Java/C++) stack traces for all threads in a Java process
Print stack traces from Java core dump
SYNOPSIS
jstack [-F] [-l] [-m] [-h] <pid>
jstack [-F] [-l] [-m] [-h] [server-id@]hostname[:port]
jstack [-F] [-l] [-m] [-h] <executable> <core>
PARAMETERS
-F
Force thread dump; use when normal attach fails (hung JVM)
-l
Long listing; adds lock info, wait objects, and owner details
-m
Mixed mode; prints Java and native (C/C++) stack frames
-h
Print help message and exit
DESCRIPTION
jstack is a command-line diagnostic tool from the JDK used to generate thread dumps for a running Java Virtual Machine (JVM) process, core file, or remote debug server. It captures the stack traces of all threads, helping diagnose issues like deadlocks, infinite loops, high CPU usage, memory leaks, and blocking operations.
Thread dumps reveal thread states (RUNNABLE, BLOCKED, WAITING), locked monitors, and contended locks. This is crucial for production troubleshooting without stopping the application.
Invoke with a process ID (PID), obtained via jps or ps. Output includes JVM version, heap summary, and per-thread details. Use options for extended info: native frames (-m), lock details (-l), or force dump (-F) on hung JVMs.
Permissions are key; run as the JVM's user or with ptrace access. Dumps are textual, analyzable by tools like Thread Dump Analyzer.
CAVEATS
Requires JDK in PATH. Needs ptrace permissions (check /proc/sys/kernel/yama/ptrace_scope on Linux). Cannot attach to JVMs denying attaches via flags like -XX:+DisableAttachMechanism. Output may be verbose; redirect to file. Incompatible with some JVM args.
OBTAINING PID
Use jps -l or ps aux | grep java to find target PID.
ANALYSIS TIPS
Look for BLOCKED/RUNNABLE threads, repeated frames, or full Monitors sections for deadlocks.
HISTORY
Introduced in JDK 6 (2006) to replace 'kill -QUIT'; enhanced in later JDKs with better remote support and native frames. Maintained in OpenJDK.


