What is thread dump in Java | How to take thread dumps from JVM?

A Java thread dump is a way of finding out what every thread in the JVM is doing at a particular point in time. This is especially useful if your Java application sometimes seems to hang when running under load, as an analysis of the dump will show where the threads are stuck.

You can generate a thread dump under Unix/Linux by running kill -QUIT <pid>, and under Windows by hitting Ctl + Break.


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 <pid>

You can output consecutive thread dumps to a file by using the console output redirect/append directive:

jstack -l <pid> >> threaddumps.log