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!!
This question is related to
java
multithreading
dump
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.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]
In the same location where the JVM's stdout is placed. If you have a Tomcat server, this will be the catalina_(date).out
file.
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
In Jboss you can perform the following
nohup $JBOSS_HOME/bin/run.sh -c yourinstancename $JBOSS_OPTS >> console-$(date +%Y%m%d).out 2>&1 < /dev/null &
kill -3 <java_pid>
This will redirect your output/threadump to the file console specified in the above command.
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
The thread dump is written to the system out of the VM on which you executed the kill -3
. If you are redirecting the console output of the JVM to a file, the thread dump will be in that file. If the JVM is running in an open console, then the thread dump will be displayed in its console.
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.
Source: Stackoverflow.com