kill -3 to get java thread dump
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.
javanerd
Updated on November 08, 2020Comments
-
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 thiskill
command? I am lost!! -
javanerd almost 13 yearsThanks 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 almost 13 yearsYes - 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 almost 11 yearsThere is a way to redirect JVM thread dump output to separate file. See in my answer.
-
noahlz about 10 yearsUntil the jstack command fails consistently due to "Unable to deduce type of thread from address" ;-(
-
Ates Goral about 10 yearsIf 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 over 9 yearsTechnically 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 almost 6 yearsIt'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 almost 6 yearsUnfortunately
jcmd
fails to connect to windows service process withcom.sun.tools.attach.AttachNotSupportedException: Insufficient memory or insufficient privileges to attach
whilejstack -F
succeeds: stackoverflow.com/questions/1197912/… -
Vadzim almost 6 yearsHere is how to use jstack to obtain thread dump of a process running under different user, like windows service: stackoverflow.com/questions/1197912/…
-
Ashley over 5 yearsI'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 over 5 yearsI 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 over 4 yearsYou 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 about 4 years@Ashley - no
kill -3 <PID>
shouldn't kill the JVM. What type of Java app are you looking at?