I am using kill -3
command to see the JVM's thread dump in unix. But where can I find the output of this kill
command? I am lost!!
kill -3 to get java thread dump
211.3k views Asked by javanerd AtThere are 10 answers
There is a way to redirect JVM thread dump output on break signal to separate file with LogVMOutput diagnostic option:
-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=jvm.log
With Java 8 in picture, jcmd
is the preferred approach.
jcmd <PID> Thread.print
Following is the snippet from Oracle documentation :
The release of JDK 8 introduced Java Mission Control, Java Flight Recorder, and jcmd utility for diagnosing problems with JVM and Java applications. It is suggested to use the latest utility, jcmd instead of the previous jstack utility for enhanced diagnostics and reduced performance overhead.
However, shipping this with the application may be licensing implications which I am not sure.
When using kill -3 one should see the thread dump in the standard output. Most of the application servers write the standard output to a separate file. You should find it there when using kill -3. There are multiple ways of getting thread dumps:
kill -3 <PID>
: Gives output to standard output.- If one has access to the console window where server is running, one can use Ctrl+Break combination of keys to generate the stack trace on STDOUT.
For hotspot VM's we can also use
jstack
command to generate a thread dump. It’s a part of the JDK. Syntax is as follows:Usage: jstack [-l] <pid> (to connect to running process) jstack -F [-m] [-l] <pid>(to connect to a hung process) - For JRockit JVM we can use JRCMD command which comes with JDK Syntax: jrcmd <jrockit pid> [<command> [<arguments>]] [-l] [-f file] [-p] -h]
Steps that you should follow if you want the thread dump of your StandAlone Java Process
Step 1: Get the Process ID for the shell script calling the java program
linux$ ps -aef | grep "runABCD"
user1 **8535** 4369 0 Mar 25 ? 0:00 /bin/csh /home/user1/runABCD.sh
user1 17796 17372 0 08:15:41 pts/49 0:00 grep runABCD
Step 2: Get the Process ID for the Child which was Invoked by the runABCD. Use the above PID to get the childs.
linux$ ps -aef | grep **8535**
user1 **8536** 8535 0 Mar 25 ? 126:38 /apps/java/jdk/sun4/SunOS5/1.6.0_16/bin/java -cp /home/user1/XYZServer
user1 8535 4369 0 Mar 25 ? 0:00 /bin/csh /home/user1/runABCD.sh
user1 17977 17372 0 08:15:49 pts/49 0:00 grep 8535
Step 3: Get the JSTACK for the particular process. Get the Process id of your XYSServer process. i.e. 8536
linux$ jstack **8536** > threadDump.log
You could alternatively use jstack (Included with JDK) to take a thread dump and write the output wherever you want. Is that not available in a unix environment?