DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • A Maven Story
  • Ways To Reduce JVM Docker Image Size
  • Keep Your Application Secrets Secret

Trending

  • Emerging Data Architectures: The Future of Data Management
  • Failure Handling Mechanisms in Microservices and Their Importance
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  1. DZone
  2. Coding
  3. Java
  4. Create Thread Dumps (Nine Options)

Create Thread Dumps (Nine Options)

Thread dumps are vital artifacts to diagnose CPU spikes, deadlocks, memory problems, unresponsive applications, poor response times, and other system problems.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Updated Sep. 15, 16 · Tutorial
Likes (44)
Comment
Save
Tweet
Share
184.7K Views

Join the DZone community and get the full member experience.

Join For Free

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 that can analyze and spot problems. But for those tools, you need to provide proper thread dumps as input. Thus, in this article, I have documented nine different options to capture thread dumps.

1. yCrash Open-Source Script

The yCrash script is a powerful open-source script that captures not only thread dumps but also 16 essential artifacts from your application to troubleshoot performance problems. Here’s how you can utilize the yCrash script to capture thread dumps and more:

  1. 1. Download the latest yCrash script
  2. 2. Unzip the downloaded yc-agent-latest.zip file. (Say you are unzipping in ‘/opt/workspace/yc-agent-latest’ folder)
  3. 3. In the unzipped folder, you will find yCrash script by operating
    system:
    a) linux/yc – If you are running on Unix/Linux, then use this script.
    b) windows/yc.exe – If you are running on Windows, then use this script.
    c) mac/yc – If you are running on MAC, then use this script.

4. You can execute the yc script using the following command:

./yc -j {JAVA_HOME} -onlyCapture -p {PID}


Where JAVA_HOME is the home directory where JDK is installed and PID is the troubled target application’s process ID.

Example:

./yc -j /usr/java/jdk1.8.0_141 -onlyCapture -p 15326


When you execute the command, yCrash script will capture all the application-level and system-level artifacts/logs from the server from the target application for analysis. Captured artifacts will be compressed into a zip file and stored in the current directory where the above command was executed. The zip file will have the name in the format: ‘yc-YYYY-MM-DDTHH-mm-ss.zip’. 

Example: ‘yc-2021-03-06T14-02-42.zip’. For more details, visit it’s GitHub repository.

2. jstack

jstack is an effective command line tool to capture thread dumps. The jstack tool is shipped in JDK_HOME\bin folder. Here is the command that you need to issue to capture the 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 thread dump will be written into.

Example:

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


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

Jstack tool is included in JDK since Java 5. If you are running an older version of Java, consider using other options

3. Kill -3

In major enterprises, for security reasons, only JREs are installed in production machines. Since jstack and other tools are only part of JDK, you wouldn’t be able to use jstack. In such circumstances, 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 the kill -3 option is used, thread dump is sent to the standard error stream. If you are running your application in Tomcat, a thread dump will be sent into <TOMCAT_HOME>/logs/catalina.out file.

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

4. 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_HOME\bin\jvisualvm.exe. It’s part of Sun’s JDK distribution since JDK 6 update 7.s

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 below diagram). This tool also has the capability to capture thread dumps from the java processes that are running in remote hosts as well.

Fig: Java Visual VMFig: Java Visual VM

Now go to the “Threads” tab. Click on the “Thread Dump” button as shown in the below image. Now, Thread dumps would be generated.

Highlighting the "Thread Dump" button in the “Threads” tab

Fig: Highlighting the "Thread Dump" button in the “Threads” tab

5. 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 into JDK since Oracle JDK 7 Update 40. This tool also provides an option to take thread dumps from the JVM. JMC tool is present in JDK_HOME\bin\jmc.exe

Once you launch the tool, you will see all the Java processes that are running on your local host. Note: JMC also has the ability to connect with Java processes running on a remote host. Now, on the left panel, click on the “Flight Recorder” option that is listed below the Java process for which you want to take thread dumps. Now you will see the “Start Flight Recording” wizard, as shown in the below figure.

Flight Recorder wizard showing ‘Thread Dump’ capture option.Fig: Flight Recorder wizard showing ‘Thread Dump’ capture option.

Here, in the “Thread Dump” field, you can select the interval in which you want to capture the 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 the recording is complete, you will see the thread dumps in the “Threads” panel, as shown in the figure below.

Showing captured ‘Thread Dump’ in JMC.

Fig: Showing captured ‘Thread Dump’ in JMC.

6. Windows (Ctrl + Break)

This option will work only in the Windows Operating system.

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

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

Note 1: 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.” In my case, it turned out that “Function key + B” is the equivalent of the “Break” key. Thus, I had to use “Ctrl + Fn + B” to generate thread dumps.

Note 2: One disadvantage of 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. 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:\workspace\threadDump.txt 2>&1


Thus, when you issue “Ctrl + Break,” a thread dump will be sent to C:\workspace\threadDump.txt file.

7. ThreadMXBean

Since JDK 1.5 ThreadMXBean has been introduced. This is the management interface for the thread system in the Java Virtual Machine. Using this interface, you 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 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());
        }
    }

8. 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), below are the instructions to capture 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 checkbox 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.

App dynamics thread dump capturing wizard

Fig: App dynamics thread dump capturing wizard

9. JCMD

The jcmd tool was introduced with Oracle’s Java 7. It’s useful in troubleshooting issues with JVM applications. It has various capabilities, such as identifying Java process Ids, acquiring heap dumps, acquiring thread dumps, and acquiring garbage collection statistics...

Using the below JCMD command, you can generate a thread dump:

jcmd <pid> Thread.print > <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 into.

Example:

jcmd 37320 Thread.print > /opt/tmp/threadDump.txt


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

Conclusion

Even though nine different options are listed to capture thread dumps, IMHO, 1. yCrash open-source script, 2. jstack and  3. kill -3 and 4. jcmd are the best ones. Because they are:

a. Simple (straightforward, easy to implement)

b. Universal (works in most cases despite OS, Java Vendor, and JVM version…)

Java (programming language) application Java virtual machine Java Development Kit Virtual Machine

Published at DZone with permission of Ram Lakshmanan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • A Maven Story
  • Ways To Reduce JVM Docker Image Size
  • Keep Your Application Secrets Secret

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!