Thread dump sampling in fixed time intervals using jstack script
This simple shell script takes several jstack snapshots in fixed time intervals: [Reference document]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/bash if [ $# -eq 0 ]; then    echo >&2 "Usage: jstackSeries [ <strong> [ </strong> ] ]"    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:
1 2 3 |
jstackSeries <strong>10 5 </strong> |
How to compare the thread dumps
To compare the results you may use interactive diff viewers, e.g.
1 |
<strong>vimdiff</strong> 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):
1 2 3 4 5 6 7 8 9 |
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.
1 |
<strong>Â </strong> |