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 [options]
PARAMETERS
pid
The process ID of the Java process to connect to.
-l
Performs long listing. Prints additional information about locks.
-m
Prints both Java and native C/C++ frames (mixed mode). Useful when the JVM uses native code.
-F
Forces a thread dump. Use when jstack
-h
Displays help message.
-help
Displays help message.
-J
Pass
DESCRIPTION
The `jstack` command is a powerful diagnostic tool used to obtain thread dumps from Java Virtual Machines (JVMs). These thread dumps provide a snapshot of the state of all threads within the JVM at a specific point in time. By analyzing these snapshots, developers can identify issues such as deadlocks, resource contention, long running threads, and other performance bottlenecks. The `jstack` output includes information about each thread's current state (e.g., running, sleeping, waiting), the stack trace which shows the sequence of method calls leading to that state, and other relevant data such as thread ID, priority, and lock information. It is commonly used in debugging production systems to pinpoint the root cause of performance degradation or application hangs.
The command attaches to a running Java process (identified by its process ID) and extracts the thread information. This information is then printed to standard output. The output can be redirected to a file for later analysis. Jstack is particularly useful when combined with other system monitoring and profiling tools to gain a comprehensive understanding of application behavior under load.
CAVEATS
The target JVM and the `jstack` command must be running with the same user ID, or you must have sufficient privileges to access the JVM process. Using the `-F` option can potentially lead to inconsistent or incomplete thread dumps, so it should be used with caution. The command is part of the JDK (Java Development Kit) and might not be available in minimal Java Runtime Environments (JREs).
INTERPRETING OUTPUT
The output of `jstack` shows each thread's name, thread ID (nid), state, and a stack trace. Common thread states include RUNNABLE (executing code), BLOCKED (waiting for a lock), WAITING (waiting indefinitely), and TIMED_WAITING (waiting for a specific duration).
Examining the stack traces associated with BLOCKED threads can often reveal the source of deadlocks or other synchronization problems.
Long running threads are easily found looking for RUNNABLE threads with a high CPU usage.
HISTORY
The `jstack` command has been part of the Java Development Kit (JDK) for many years. It was originally designed to help diagnose problems in Java applications by providing insight into the state of threads. Over time, the command has been enhanced with features such as lock information and mixed-mode stack traces to support increasingly complex Java applications. Its usage remains a core skill for Java developers and system administrators troubleshooting production issues.