Java Thread Dumps - Documentation topics on: java,thread dump,.

Java Thread Dumps

Getting Java Thread Dumps

It can be useful when experiencing problems with a java application to get thread dumps, as it provides a detailed look at what is happening in the JVM at the time the dump is captured.  Typically it is most useful to grab a number of dumps over a period of time, such as 10 of them spaced 10 seconds apart.  This gives a fuller picture of what is actually problematic and what items that could appear to be an issue are actually not.  If your dotCMS instance is hung, appears to be crashed, or is generally unresponsive then before shutting it down and restarting it is helpful to grab several thread dumps for further research.

On Linux, Mac OS X, or other UNIX like systems

A script like the following would be an effective approach (edited for your particular paths and the PID of dotcms)



while [ $COUNTER -lt 10 ]; do
export TIMESTAMP=$(date +%Y%m%d%H%M%S)
echo "Thread CPU stats for Dotcms process $(cat /tmp/" >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
echo -n " Memory: " >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
ps --pid $(cat /tmp/ -o rss |grep -E "^\s*[0-9]+" | xargs -i% echo "% / 1024" | bc | xargs printf "%s MB\n" >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
echo -n " Threads: " >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
ps --pid $(cat /tmp/ -o nlwp |grep -E "^\s*[0-9]+" >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
echo "TID %CPU CPUTIME" >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
ps --pid $(cat /tmp/ -L -o tid,pcpu,cputime |grep -E "^\s*[0-9]+" |sort -rn -k 2 |xargs -n 3 printf '0x%x\t%s\t%s\n' >>/opt/dotcms/logs/tomcat/thread_cpu_$(hostname -s)_$TIMESTAMP.txt
$JAVA_HOME/bin/jstack -l $(cat /tmp/ > /opt/dotcms/logs/tomcat/threaddump_$(hostname -s)_$TIMESTAMP.txt
sleep 5

On Windows

The best option is in conjunction with Microsoft's psExec tool.  Use the following command to execute this tool (edited for your particular paths and the PID of dotcms):

C:\PSTools>psexec -s c:\Java\jdk1.7.0_51\bin\jstack.exe -l 4180 >dump.txt