- Capturing a Java Thread Dump
- Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:
- 1. Overview
- 2. Using JDK Utilities
- 2.1. jstack
- 2.2. Java Mission Control
- 2.3. jvisualvm
- 2.4. jcmd
- 2.5. jconsole
- 2.6. Summary
- 3. From the Command Line
- 3.1. kill -3 Command (Linux/Unix)
- 3.2. Ctrl + Break (Windows)
- 4. Programmatically Using ThreadMxBean
- 5. Conclusion
- Take thread dumps from a JVM
- How can I take thread dumps from a JVM on Linux, UNIX or Windows?
- Step 1: Get the PID of your Java process
- Step 2: Request a thread dump from the JVM
- jstack script
- Alternative ways to obtain a thread dump
Capturing a Java Thread Dump
Last modified: October 10, 2020
Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:
If you have a few years of experience in the Java ecosystem, and you’re interested in sharing that experience with the community (and getting paid for your work of course), have a look at the «Write for Us» page. Cheers, Eugen
1. Overview
In this tutorial, we’ll discuss various ways to capture the thread dump of a Java application.
A thread dump is a snapshot of the state of all the threads of a Java process. The state of each thread is presented with a stack trace, showing the content of a thread’s stack. A thread dump is useful for diagnosing problems as it displays the thread’s activity. Thread dumps are written in plain text, so we can save their contents to a file and look at them later in a text editor.
In the next sections, we’ll go through multiple tools and approaches to generate a thread dump.
2. Using JDK Utilities
The JDK provides several utilities that can capture the thread dump of a Java application. All of the utilities are located under the bin folder inside the JDK home directory. Therefore, we can execute these utilities from the command line as long as this directory is in our system path.
2.1. jstack
jstack is a command-line JDK utility we can use to capture a thread dump. It takes the pid of a process and displays the thread dump in the console. Alternatively, we can redirect its output to a file.
Let’s take a look at the basic command syntax for capturing a thread dump using jstack:
All the flags are optional. Let’s see what they mean:
- -F option forces a thread dump; handy to use when jstack pid does not respond (the process is hung)
- -l option instructs the utility to look for ownable synchronizers in the heap and locks
- -m option prints native stack frames (C & C++) in addition to the Java stack frames
Let’s put this knowledge to use by capturing a thread dump and redirecting the result to a file:
Remember that we can easily get the pid of a Java process by using the jps command.
2.2. Java Mission Control
Java Mission Control (JMC) is a GUI tool that collects and analyzes data from Java applications. After we launch JMC, it displays the list of Java processes running on a local machine. We can also connect to remote Java processes through JMC.
We can right-click on the process and click on the “Start Flight Recording” option. After this, the Threads tab shows the Thread Dumps:
2.3. jvisualvm
jvisualvm is a tool with a graphical user interface that lets us monitor, troubleshoot, and profile Java applications. The GUI is simple but very intuitive and easy to use.
One of its many options allows us to capture a thread dump. If we right-click on a Java process and select the “Thread Dump” option, the tool will create a thread dump and open it in a new tab:
As of JDK 9, Visual VM is not included in the Oracle JDK and Open JDK distributions. Therefore, if we’re using Java 9 or newer versions, we can get the JVisualVM from the Visual VM open source project site.
2.4. jcmd
jcmd is a tool that works by sending command requests to the JVM. Although powerful, it doesn’t contain any remote functionality – we have to use it in the same machine where the Java process is running.
One of its many commands is Thread.print. We can use it to get a thread dump just by specifying the pid of the process:
2.5. jconsole
jconsole lets us inspect the stack trace of each thread. If we open jconsole and connect to a running Java process, we can navigate to the Threads tab and find each thread’s stack trace:
2.6. Summary
So as it turns out, there are many ways to capture a thread dump using JDK utilities. Let’s take a moment to reflect on each and outline their pros and cons:
- jstack: provides the quickest and easiest way to capture a thread dump. However, better alternatives are available starting with Java 8
- jmc: enhanced JDK profiling and diagnostics tool. It minimizes the performance overhead that’s usually an issue with profiling tools
- jvisualvm: lightweight and open-source profiling tool with an excellent GUI console
- jcmd: extremely powerful and recommended for Java 8 and later. A single tool that serves many purposes – capturing thread dump (jstack), heap dump (jmap), system properties and command-line arguments (jinfo)
- jconsole: let’s us inspect thread stack trace information
3. From the Command Line
In enterprise application servers, only the JRE is installed for security reasons. Thus, we can not use the above-mentioned utilities as they are part of JDK. However, there are various command-line alternatives that let us capture thread dumps easily.
3.1. kill -3 Command (Linux/Unix)
The easiest way to capture a thread dump in Unix-like systems is through the kill command, which we can use to send a signal to a process using the kill() system call. In this use case, we’ll send it the -3 signal.
Using our same pid from earlier examples, let’s take a look at how to use kill to capture a thread dump:
This way the signal-receiving Java process will print the thread dump on the standard output.
If we run the Java process with the following combination of tuning flags, then it will also redirect the thread dump to the given file:
Now if we send the -3 signal, in addition to the standard output, the dump will be available at
3.2. Ctrl + Break (Windows)
In Windows operating systems, we can capture a thread dump using the CTRL and Break key combination. To take a thread dump, navigate to the console used to launch the Java application and press CTRL and Break keys together.
It’s worth noting that, on some keyboards, the Break key is not available. Therefore, in such cases, a thread dump can be captured using CTRL, SHIFT, and Pause keys together.
Both of these commands print the thread dump to the console.
4. Programmatically Using ThreadMxBean
The last approach we will discuss in the article is using JMX. We’ll use ThreadMxBean to capture the thread dump. Let’s see it in code:
In the above program, we are performing several steps:
- At first, an empty StringBuffer is initialized to hold the stack information of each thread.
- We then use the ManagementFactory class to get the instance of ThreadMxBean. A ManagementFactory is a factory class for getting managed beans for the Java platform. In addition, a ThreadMxBean is the management interface for the thread system of the JVM.
- Setting lockedMonitors and lockedSynchronizers values to true indicates to capture the ownable synchronizers and all locked monitors in the thread dump.
5. Conclusion
In this article, we’ve shown multiple ways to capture a thread dump.
At first, we discussed various JDK Utilities and then the command-line alternatives. In the last section, we concluded with the programmatic approach using JMX.
As always, the full source code of the example is available over on GitHub.
Take thread dumps from a JVM
На этой странице
某些 Creative Cloud 应用程序、服务和功能在中国不可用。
How can I take thread dumps from a JVM on Linux, UNIX or Windows?
A thread dump is a list of all the Java threads that are currently active in a Java Virtual Machine (JVM).
There are several ways to take thread dumps from a JVM. It is highly recommended to take more than 1 thread dump. A good practice is to take 10 thread dumps at a regular interval (for example, one thread dump every ten seconds).
Step 1: Get the PID of your Java process
The first piece of information you will need to be able to obtain a thread dump is your Java process’s PID.
The Java JDK ships with the jps command which lists all Java process ids. You can run this command like this:
jps -l 70660 sun.tools.jps.Jps 70305
Note: In Linux and UNIX, you may have to run this command as sudo -u user jps -l , where «user» is the username of the user that the Java process is running as.
If this doesn’t work or you still cannot find your Java process, (path not set, JDK not installed, or older Java version), use
- UNIX, Linux, and Mac OS X: ps -el | grep java
- Windows: Press Ctrl+Shift+Esc to open the task manager and find the PID of the Java process
Step 2: Request a thread dump from the JVM
jstack
If installed/available, we recommend using the jstack tool. It prints thread dumps to the command line console.
To obtain a thread dump using jstack, run the following command:
jstack -l
You can output consecutive thread dumps to a file by using the console output redirect/append directive:
jstack -l
- The jstack tool is available since JDK 1.5 (for JVM on Windows, it’s available in some versions of JDK 1.5 and JDK 1.6 only).
- jstack works even if the -Xrs jvm parameter is enabled
- It’s not possible to use the jstack tool from JDK 1.6 to take threaddumps from a process running on JDK 1.5.
- In Linux and UNIX, you need to run the command as the user that owns the java process:
sudo -u java-user jstack -l
( should be replaced with the id of the user that the Java process is running as) - In Windows, if you run jstack and get the error «Not enough storage is available to process this command» then you must run jstack as the windows SYSTEM user or the user that owns the java process. You can do this by using psexec which you can download here. To run jstack as SYSTEM user, use a command like this:
psexec -s jstack >> threaddumps.log
If you are unable to install psexec on the server, then you can create a .bat file containing the command and run it using the Windows task scheduler (as a different user). - If the java process isn’t responding, then it can sometimes help to use the option -J-d64 (on 64 bit systems), for example:
jstack -J-d64 -l
>> threaddumps.log
If the jstack command ( jstack -l
>> threaddumps.log ) throws the error [1] below, then run the command as the user that owns the java process. For example:
sudo -u sling jstack -l
jstack script
Here’s a script (adapted from the one on eclipse.org) that will take a series of thread dumps using jstack. It also takes the thread level cpu usage using top command as well.
Just run it like this:
sudo -u jstackSeries.sh
For example:
sudo -u aemuser jstackSeries.sh 1234 aemserveruser 10 3
- 1234 is the pid of the Java process
- cq5serveruser is the Linux or UNIX user that the Java process runs as
- 10 is how many thread dumps to take
- 3 is the delay between each dump
Note: The top output has the native thread id in decimal format while the jstack output has the nid in hexadecimal. You can match the high cpu thread from the top output to the jstack output by converting the thread id to hexadecimal.
Thread Dump Tool for Adobe Experience Manager
If you are using Adobe Experience Manager product then you can install this tool to have a simple UI for generating thread dumps.
Alternative ways to obtain a thread dump
If the jstack tool is not available to you then you can take thread dumps as follows:
Note: Some tools cannot take thread dumps from the JVM if the commandline parameter -Xrs is enabled. If you are having trouble taking thread dumps then please see if this option is enabled.
UNIX, Mac OS X, and Linux (JDK 1.4 or lesser version)
On UNIX, Mac OS X, and Linux, you can send a QUIT signal to the Java process to tell it to output a thread dump to standard output.
- Run this command to do this:
kill -QUIT
where «user» is the user that the Java process is running as.
Windows:
JDK 1.X
- Download javadump.exe (attached below).
- Start the JVM with these three arguments (they must be in the right order):
-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=C:\temp\jvmoutput.log - Press Ctrl+Shift+Esc to open the Task Manager.
- Find the PID of the Java process.
- From the command line, run
javadump.exe
JDK 1.6
Get a thread dump from jconsole tool, by using a plugin: [0]
Here’s how you can request a thread dump:
- Add the following parameter to the jvm running Communique : -Dcom.sun.management.jmxremote
- Download and install JDK 1.6 (if not done yet).
- Download and extract the Thread Dump Analyzer utility. [1]
- Run jconsole.exe of JDK 1.6:
jconsole.exe -pluginpath /path/to/file/tda.jar - Click the Thread dumps tab.
- Click the Request Thread Dump link.
Note: If you are running AEM 6.* and want to observe the running threads, you can request http:// :
/system/console/status-Threads to get a thread list. However, please note that these thread dumps will not work with thread dump analysis tools such as samurai or tda.