HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Linux / JStack thread dump analyzer

JStack thread dump analyzer

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 while analyzing any problem such as deadlock or resource usage analysis. It is always better to confirm in more than one thread dump then making conclusions in single attempt.

1. Get the PID of 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

Remember – you may have to run this command as $ sudo -u jps -l, where “user” is the username of the user that the java process is running as.

Even now, if you are not able to find out process id, use below command:

$ ps -aef | grep java

2. Request a Thread Dump from the JVM

If installed/available, we recommend using the jstack tool. It prints thread dumps to the command line console.

To obtain thread dump using jstack, run the following command:

$ jstack

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

$ jstack  >> threaddumps.log

Important points

  1. The jstack tool is available since JDK 1.5.
  2. jstack works even if the -Xrs jvm parameter is enabled.
  3. It’s not possible to use the jstack tool from JDK 1.6 to take threaddumps from a process running on JDK 1.5.

3. Thread dump sampling in fixed time intervals using jstack script

This simple shell script takes several jstack snapshots in fixed time intervals: [Reference document]

#!/bin/bash

if [ $# -eq 0 ]; then
    echo >= 2 "Usage: jstackSeries  [ count [ delay ] ]"
    echo >= 2 "    Defaults: count = 10, delay = 1 (seconds)"
    exit 1
fi

pid=$1          # required
count=${2:-10}  # defaults to 10 times
delay=${3:-1} # defaults to 1 second

while [ $count -gt 0 ]
do
    jstack $pid >jstack.$pid.$(date +%H%M%S.%N)
    sleep $delay
    let count--
    echo -n "."
done

Use above script like this:

$ jstackSeries  10 5

4. How to compare two JStack thread dumps

To compare thread dumps you may use interactive difference viewers, e.g.

$ vimdiff file1 file2 file3 file4 # up to 4 files

Another way to see what parts of the jstack trace are changing over time is to compare adjacent jstack trace using $ context diff (-c option):

d_old=""
for d in jstack.13585.12171*
do
  if [ -n "$d_old" ]
  then
    diff -c "$d_old" "$d"
  fi
  d_old="$d"
done

Here, the result shows only the places where the JStack thread dump changes from file to file.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. satish

    June 21, 2017

    we have installed java 1.7 but when we use jstack command it is saying as command not found
    we need to install jstack package using yum or rpm, also we need to set the JAVA_HOME and PATH
    Also if we want to set permanently what is the path
    Thanks

  2. chakri

    December 13, 2013

    Hi..
    My application is running on java 5 and when I tried to run jstack command it is saying command not found.

    I searched in /opt/java 1.5/bin folder I couldn’t see any jstack command.

    Where as when I see in java 6/bin folder I can see jstack command.

    Can you suggest me how to proceed.

    Chakri
    chakri.950@gmail.com

    • Lokesh Gupta

      December 13, 2013

      you need to upgrade java version.

  3. Prem

    November 14, 2013

    PID was missed

    jstack pid >>threaddumps.log

  4. Prem

    November 14, 2013

    We missed to mention

  5. Prem

    November 14, 2013

    Hi

    I think this should be the statement.

    jstack >> threaddumps.log

  6. pintu

    July 5, 2013

    Thanks Dear for sharing such a nice information !!

  7. Vishesh

    July 5, 2013

    Great information, dude !!

  8. sayyapillai

    December 20, 2012

    Thanks for your grateful informations, am working in FNT software solutions, so it will be helpful info for my works.

Comments are closed on this article!

Search Tutorials

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)