Skip to main content

How to Capture and Analyze a Thread Dump

We’ll explore how to capture a thread dump and analyze it effectively in this post. This is a continuation of Java Dumps Uncovered: When to Use Thread Dump, Heap Dump, or Core Dump.

Quick Summary

thread dump helps determine if a thread is stuck. However, taking a single thread dump isn’t enough—we need at least two or more dumps to confirm whether a thread remains in the same state over time.


Ways to Capture a Thread Dump

There are multiple ways to take a thread dump. We’ll explore each method using a sample Java program that creates two threads—one terminating in 30 seconds and another in 5 minutes. The Java file is available in this gist.

To capture a thread dump, the first step is to identify the Java process ID (PID). This can be done using:

$ jps

or

$ ps aux | grep java

This command lists all running Java processes. Select the PID of the process for which you need the thread dump.


1) Using jstack or jcmd

The jstack command prints the thread dump of a running Java process:

$ jstack <PID> 

To save the output to a file:

$ jstack <PID> > threaddump.txt

Alternatively, the jcmd command can be used:

$ jcmd <PID> Thread.print

Or to save it to a file:

$ jcmd <PID> Thread.print > threaddump.txt
Example Screenshot:

The thread dump appears in the same directory where the command is executed.


2) Using kill -3 (Linux/macOS)

This method sends a SIGQUIT signal to the JVM, causing the thread dump to be written to the application’s standard error output (stderr) or logs:

$ kill -3 <PID>

The thread dump appears in the console output or application logs.


3) Using VisualVM

To capture a thread dump using VisualVM:

  1. Start VisualVM (jvisualvm command).
  2. Select the Java process for which the thread dump is needed.
  3. Click on Applications from the menu bar and select Thread Dump.

4) Using jconsole

To capture a thread dump with jconsole:

  1. Start jconsole from the terminal.
  2. Choose the application from Local Processes or connect to a remote machine.
  3. Navigate to the Threads tab.
  4. View all running threads in the left panel.
  5. If a thread has been running for an extended period, click on it to view the thread dump.

Which Method to Use?

In most cases, jstack/jcmd and kill -3 are preferred since VisualVM and jconsole cannot connect to production machines due to security restrictions.

Additionally, tools like Java Mission Control can capture thread dumps. Some application servers like Tomcat, WebLogic, and WebSphere provide admin tools to generate thread dumps, but disabling these tools is recommended for security reasons. 😉


Conclusion

Understanding how to take a thread dump is essential for diagnosing application issues. Whether using jstack, jcmd, kill -3, VisualVM, or jconsole, selecting the right method depends on the environment. Capturing multiple dumps and analyzing them carefully can help identify stuck threads and performance bottlenecks, ensuring smoother application performance. 🚀


Comments

Popular posts from this blog

Java Dumps Uncovered: When to Use Thread Dump, Heap Dump, or Core Dump

Choosing the Right Dump for the Right Problem: Knowing which dump to use in a given situation provides a significant advantage while debugging. In this blog, we’ll explore the differences between Thread Dump, Heap Dump, and Core Dump , how to capture them, and when to use them effectively. Understanding Java Threads and Memory: When a simple Java class with a main method runs, it creates a single thread named "main" , which executes the program. In many cases, this single thread is sufficient. However, for applications handling multiple tasks simultaneously, multiple threads are necessary. For example, a web server like Tomcat would be ineffective if it handled all requests using a single thread. Instead, it creates multiple threads to manage concurrent requests. Alongside threads, Java runtime also requires memory to execute programs, which includes components like the Method Area, Heap, Stack, etc. When to Take a Thread Dump? Imagine running a Tomcat server , and a...

How to Capture and Analyze a Heap Dump

In this post, we'll dive into the practical aspects of capturing and analyzing Java heap dumps. This article continues from our previous deep dive, Java Dumps Uncovered: When to Use Thread Dump, Heap Dump, or Core Dump — where we explored the scenarios in which each type of dump is appropriate. Quick Summary Refer to the section on When to Take a Heap Dump from the previous post to understand the right circumstances for capturing heap dumps — such as frequent OutOfMemoryError s or excessive memory retention issues. Ways to Capture a Heap Dump There are several ways to generate a heap dump in Java. In this post, we'll demonstrate these using a sample Java application that consumes significant memory via ArrayList , HashMap , and other objects. You can find the sample code in this GitHub Gist . Before you can capture a heap dump, you'll need to identify the Java process ID (PID). You can do this using either of the following commands: $ jps or $ ps aux | grep java Once you ...