JavaData & JavaSeven Options on For Taking Java Thread Dumps

Seven Options on For Taking Java Thread Dumps

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

By Ram Lakshmanan

Thread dumps are vital artifacts to diagnose CPU spikes, deadlocks, memory problems, unresponsive applications, poor response times, and other system problems. There are great online thread dump analysis tools, such as http://fastthread.io/, which can analyse and spot problems. But, to use those tools, you need provide proper thread dumps as input. Thus in this article, I have documented seven different options to capture thread dumps.

1. jstack

‘jstack’ is an effective command line tool to capture thread dumps. The jstack tool is shipped in the JDK_HOMEbin folder. Here is the command that you need to issue to capture a thread dump:

jstack -l  <pid> > <file-path>

where

pid: is the Process Id of the application, whose thread dump should be captured.

file-path: is the file path where the thread dump will be written.

Example:

jstack -l 37320 > /opt/tmp/threadDump.txt

As per the example, a thread dump of the process would be generated in the /opt/tmp/threadDump.txt file.

The jstack tool has been included in the JDK since Java 5. If you are running an older version of Java, consider using other options.

2. Kill -3

In major enterprises, for security reasons, only JREs are installed on production machines. Because jstack and other tools are only part of the JDK, you wouldn’t be able to use the jstack tool. In such circumstances, the ‘kill -3’ option can be used.

kill -3 <pid>

where

pid: is the Process Id of the application, whose thread dump should be captured.

Example:

Kill -3 37320

When ‘kill -3’ option is used, a thread dump is sent to the standard error stream. If you are running your application in tomcat, the thread dump will be sent to the <TOMCAT_HOME>/logs/catalina.out file.

Note: To my knowledge, this option is supported in most flavours of *nix operating systems (Unix, Linux, HP-UX operating systems). I’m not sure about other operating systems.

3. JVisualVM

Java VisualVM is a graphical user interface tool that provides detailed information about the applications while they are running on a specified Java Virtual Machine (JVM). It’s located in JDK_HOMEbinjvisualvm.exe. It’s been part of Sun’s JDK distribution since JDK 6 update 7.

Launch the jvisualvm. On the left panel, you will notice all the Java applications that are running on your machine. You need to select your application from the list (see the red color highlight in the Figure 1). This tool also has the ability to capture thread dumps from the Java processes that are running in the remote host as well.

Dump1
Figure 1: Java Visual VM

Now, go to the “Threads” tab. Click the “Thread Dump” button, as shown in Figure 2. Now, thread dumps would be generated.

Dump2
Figure 2: Highlighting the “Thread Dump” button in the “Threads” tab

4. JMC

Java Mission Control (JMC) is a tool that collects and analyzes data from Java applications running locally or deployed in production environments. This tool has been packaged in the JDK since Oracle JDK 7 Update 40. This tool also provides an option to take thread dumps from the JVM. The JMC tool is present in JDK_HOMEbinjmc.exe.

Once you launch the tool, you will see all the Java processes that are running on your local host.

Note: JMC also has ability to connect with Java processes running on the remote host.

Now, on the left panel click the “Flight Recorder” option that is listed below the Java process for which you want to take thread dumps. You will see the “Start Flight Recording” wizard, as shown in Figure 3.

Dump3
Figure 3: Flight Recorder wizard showing the ‘Thread Dump’ capture option

Here in the “Thread Dump” field, you can select the interval in which you want to capture thread dump. As per the above example, every 60 seconds a thread dump will be captured. After the selection is complete, start the Flight recorder. Once recording is complete, you will see the thread dumps in the “Threads” panel, as shown in Figure 4.

Dump4
Figure 4: Showing the captured ‘Thread Dump’ in JMC

5. Windows (Ctrl + Break)

This option will work only in the Windows operating system.

  1. Select the command line console window in which you have launched the application.
  2. Now, on the console window issue the “Ctrl + Break” command.

This will generate the thread dump. The thread dump will be printed on the console window itself.

Note: In several laptops (like my Lenovo T series), the “Break” key is removed. In such circumstances, you have to Google to find the equivalent keys for the “Break” key. In my case, it turned out that “Function + B” is the equivalent of the “Break” key. Thus, I had to use “Ctrl + Fn + B” to generate thread dumps.
Note: But, one disadvantage with this approach is that the thread dump will be printed on the Windows console itself. Without getting the thread dump in a file format, it’s hard to use the thread dump analysis tools, such as http://fastthread.io. Thus, when you launch the application from the command line, redirect the output to a text file. For example, if you are launching the application “SampleThreadProgram,” you would issue the command:
java -classpath . SampleThreadProgram

instead launch the SampleThreadProgram like this

java -classpath . SampleThreadProgram >
   C:workspacethreadDump.txt 2>&1

Thus, when you issue “Ctrl + Break,” the thread dump will be sent to the C:workspacethreadDump.txt file.

6. ThreadMXBean

Since JDK 1.5, ThreadMXBean has been introduced. This is the management interface for the thread system in the Java Virtual Machine. By using this interface, you also can generate thread dumps. You only have to write a few lines of code to generate thread dumps programmatically. Below is a skeleton implementation on the ThreadMXBean implementation, which generates a thread dump from the application.

   public void  dumpThreadDump() {
      ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
      for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
         System.out.print(ti.toString());
      }
   }

7. APM Tool— App Dynamics

Few Application Performance Monitoring tools provide options to generate thread dumps. If you are monitoring your application through App Dynamics (APM tool), following are the instructions to capture a thread dump:

  1. Create an action, selecting Diagnostics->Take a thread dump in the Create Action window.
  2. Enter a name for the action, the number of samples to take, and the interval between the thread dumps in milliseconds.
  3. If you want to require approval before the thread dump action can be started, check the “Require approval before this Action” check box and enter the email address of the individual or group that is authorized to approve the action. See Actions Requiring Approval for more information.
  4. Click OK.

Dump5
Figure 5: App dynamics thread dump capturing wizard

Conclusion

Even though seven different options are listed to capture thread dumps, IMHO, 1. ‘jstack’ and 2. ‘kill -3’ are the best ones because they are:

  1. Simple (straightforward, easy to implement)
  2. Universal (works in most of cases despite of OS, Java Vendor, JVM version, …)

About the Author

Every single day, millions & millions of people in North America—bank, travel, and commerce—use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.

This article was contributed for exclusive use on Developer.com. All rights reserved.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories