How to Create a Java Thread Dump Using Command Line
A Java thread dump is a snapshot of all active threads in a Java application, useful for diagnosing performance issues, deadlocks, or application hangs. This tutorial explains how to generate a thread dump using the command line with JDK 22.
1. Identify the Java Process (PID)
Before generating a thread dump, you need to find the Process ID (PID) of the running Java application.
Method 1: Using jps
JDK provides the jps
(Java Process Status) tool to list active Java processes:
This command outputs a list of Java processes with their PIDs. Example output:
12345 my.application.Main
67890 org.apache.catalina.startup.Bootstrap
Here, 12345
is the PID of the Java application.
Method 2: Using ps
(Linux/macOS)
If jps
is unavailable, use the ps
command:
This will list all Java processes along with their PIDs.
2. Generate the Thread Dump
Once you have identified the PID of the Java process, use one of the following methods to generate a thread dump.
Method 1: Using jstack
(Recommended)
jstack
is a built-in JDK tool that provides a detailed thread dump.
Example:
This saves the thread dump into a file for further analysis.
Method 2: Using kill -3
(Linux/macOS)
You can send the SIGQUIT
signal to the Java process, which causes the JVM to print a thread dump to the standard output (console or logs).
Example:
Check the application logs or console output for the generated dump.
Method 3: Using jcmd
The jcmd
tool is another JDK utility that can generate a thread dump.
Example:
This saves the thread dump into a file.
3. Analyze the Thread Dump
After generating the thread dump, you can analyze it using tools like:
- Thread Dump Analyzer (TDA)
- Eclipse MAT (Memory Analyzer Tool)
- VisualVM (includes a thread dump viewer)
- Online thread dump analysis tools (e.g., fastthread.io)
To find out possible issues:
- Look for “BLOCKED” or “WAITING” states for deadlocks.
- Check for high CPU-consuming threads (often marked as “RUNNABLE”).
- Identify long-running operations.
Conclusion
Generating a Java thread dump is crucial for troubleshooting performance bottlenecks and diagnosing deadlocks. Using JDK 22, you can create a thread dump easily with jstack
, jcmd
, or kill -3
commands.