How to get thread dump in linux using jstack

本文介绍如何获取Java应用程序的线程Dump,并通过多个Dump对比分析来诊断死锁和资源使用问题。文章详细说明了如何使用jps和jstack工具获取线程Dump,并提供了一个用于定时获取Dump的Shell脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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

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

Step 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 a 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.

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  [  [  ] ]"
    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

How to compare the thread dumps

To compare the results you may use interactive diff 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 trace changes from file to file.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值