kill -3 to get java thread dump

190,247

Solution 1

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

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

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

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

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.

Share:
190,247
javanerd
Author by

javanerd

Updated on November 08, 2020

Comments

  • javanerd
    javanerd about 3 years

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

  • javanerd
    javanerd almost 13 years
    Thanks for that.Absolutely!! Yes I can use this. jstack PID > outfile will output the thread dump at that particular period of time. Isn't it?
  • Ates Goral
    Ates Goral almost 13 years
    Yes - at the point in time it is run. You can also specify -l (lowercase L) for a long listing that prints additional lock information
  • Vadzim
    Vadzim almost 11 years
    There is a way to redirect JVM thread dump output to separate file. See in my answer.
  • noahlz
    noahlz about 10 years
    Until the jstack command fails consistently due to "Unable to deduce type of thread from address" ;-(
  • Ates Goral
    Ates Goral about 10 years
    If you're seeing that error, I suggest taking it up with your vendor. A quick search shows, for example, there is an open bug in RHEL regarding this error and openjdk...
  • sqweek
    sqweek over 9 years
    Technically this doesn't "redirect" the thread dump output. It turns on JVM logging into jvm.log (which includes thread dump output) but kill -QUIT will still dump to the process's stdout (aswell). Upvoted for the description of obscure JVM options :)
  • jeffkempf
    jeffkempf almost 6 years
    It's worth noting that jstack requires the JDK. If you're running apps on a server that only has the JRE installed, you'll need to find another means for thread dumping.
  • Vadzim
    Vadzim almost 6 years
    Unfortunately jcmd fails to connect to windows service process with com.sun.tools.attach.AttachNotSupportedException: Insufficient memory or insufficient privileges to attach while jstack -F succeeds: stackoverflow.com/questions/1197912/…
  • Vadzim
    Vadzim almost 6 years
    Here is how to use jstack to obtain thread dump of a process running under different user, like windows service: stackoverflow.com/questions/1197912/…
  • Ashley
    Ashley over 5 years
    I'm having trouble using Kill -3 <PID>. It works ok but kills the process also after writing thread dump to console. Is it supposed to do that?
  • JinnKo
    JinnKo over 5 years
    I found this didn't result in the dump being sent to the STDOUT of jstack but the STDOUT of the java process instead, possibly because withtout the -F flag the request failed, then adding the -F may have caused the process to do this.
  • Twilite
    Twilite over 4 years
    You need to run jcmd <pid> Thread.dump under the same user as java process has, otherwise your connections will be dropped. See stackoverflow.com/questions/25438983/…
  • Marco
    Marco about 4 years
    @Ashley - no kill -3 <PID> shouldn't kill the JVM. What type of Java app are you looking at?