kill -3 to get java thread dump

JavaMultithreadingDump

Java Problem Overview


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!!

Java Solutions


Solution 1 - Java

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?

jstack PID > outfile

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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]
    

Solution 7 - Java

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.

Solution 8 - Java

  1. Find the process id [PS ID]
  2. Execute jcmd [PS ID] Thread.print

Solution 9 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjavanerdView Question on Stackoverflow
Solution 1 - JavaJoshua McKinnonView Answer on Stackoverflow
Solution 2 - JavaKris BabicView Answer on Stackoverflow
Solution 3 - JavaVadzimView Answer on Stackoverflow
Solution 4 - JavaArnab BiswasView Answer on Stackoverflow
Solution 5 - JavaDanielView Answer on Stackoverflow
Solution 6 - JavaApoorveView Answer on Stackoverflow
Solution 7 - JavaanishView Answer on Stackoverflow
Solution 8 - JavaMehmet ErdemsoyView Answer on Stackoverflow
Solution 9 - JavaNickView Answer on Stackoverflow