How to Obtain a Thread Dump (Stack Traces) from a Java Process or from a Core File of a Java Process on Linux
Applies To
Java SE JDK and JRE - Any Version
Generic Linux
Introduction
On Linux, there are multiple ways to obtain a Thread Dump (a Stack Trace for each thread) from a Java process, or from a core file of a Java process.
| Tool | Works on a Process? | Works on a core file? | Java Frames?* | Native Frames?** |
|---|---|---|---|---|
| jstack | yes | yes | yes | only if -m is set |
| jcmd | yes | no | yes | no |
| kill -QUIT, kill -3 | yes | no | yes | no |
| Ctrl + \ | yes | no | yes | no |
| jconsole | yes | no | yes | no |
| jvisualm | yes | no | yes | no |
| gdb | yes | yes | no | yes |
*‘Java Frames’ means that the tool is able to print a Java method name
on the stack.
**‘Native Frames’ means that the tool is able to print symbolic or
clear text name of a native function on the stack.
jstack
On Linux, jstack has been available since JDK 5. It is not available
in the Java Runtime Environment (JRE). It is also not available on the
Linux Itanium platform.
The jstack command prints the stack traces of all Java threads in a
given Java process or core file. The -m option will print a mixed-mode
stack so that you can see native frames in addition to the Java frames.
Java thread names aren’t printed if mixed-mode is enabled.
jstack [option] <PID>
jstack [option] executable core
jstack [option] [server-id@]remote-hostname-or-IP
For more information on jstack, see the jstack
Documentation.
jcmd
On Linux, jcmd has been available from JDK 7u4. It is not available in
the JRE.
The jcmd command can print the stack traces of all Java threads in a
given Java process. The -l option will also print
java.util.concurrent locks.
jcmd <pid> Thread.print [-l]
For more information on jcmd, see the documentation for your version:
kill -QUIT and kill-3
The /bin/kill command is bundled with most Linux derivates.
You can send a SIGQUIT signal to the target process. In that case the
target process prints a thread dump to its standard output. The kill
command allows you to specify the signal as a name without the SIG
prefix:
kill -QUIT <PID>
or as a number (the value for the SIGQUIT signal is 3):
kill -3 <PID>
Make sure that you know where the log file is if standard output has been redirected.
Ctrl+\
On Linux, the combination of pressing the Ctrl key and the backslash (\ key at the application console (standard input) causes the Java HotSpot VM to print a thread dump to the application’s standard output.
JConsole
On Linux, the JConsole has been available since JDK 5. It is not available in the JRE. Note that the program is called JConsole, and that the Java Console is something different.
JConsole provides a graphical user interface and its purpose is to monitor a running Java process.
In order to obtain a stack trace, connect JConsole to the process and click on the Threads tab. The Threads list in the lower left corner lists all Java threads. Click on the name of a thread in the Threads list to display the stack trace about that thread to the right.
See Also
Java VisualVM (jvisualvm)
javavisualvm was first bundled with Java SE in JDK 6u7. It is not
available in the JRE. Similarly to JConsole, Java VisualVM provides a
graphical user interface.
In order to obtain a stack trace, connect Java VisualVM to the process and click on the Threads Tab. The Threads list lists all Java threads. Click on the Thread Dump button in order to obtain a thread dump.
See Also
gdb
On Linux, gdb (the GNU Debugger) can be used to debug the Java HotSpot
VM because it is compiled using gcc (the GNU C++ Compiler). gdb is
available on most Linux derivates by default. It is a general purpose
debugger and it can obtain stack traces from both core files and live
processes. In order to avoid privilege problems, it helps to run gdb
with root privileges:
sudo gdb executable { core | pid }
Once attached successfully you can work with the gdb commands. The
prompt is set to (gdb). In order to get a list of the threads enter:
(gdb) info threads
An asterisk ’*’ to the left of the gdb thread number indicates the
current thread. You can select a thread by typing “thread” followed by
the thread id:
(gdb) thread 1
In order to get a stack trace of the current thread (in the example it
is thread #1), just enter bt or backtrace:
(gdb) bt
In order to get stack traces for all threads, you can type:
(gdb) thread apply all bt
You can execute gdb in non-interactive mode as well:
gdb --pid=<PID> --batch -ex "thread apply all bt" Last reviewed on Sat Feb 01 2025 00:00:00 GMT+0000 (Coordinated Universal Time)