How to Capture Java Thread Dumps Using JDK Tools, OS Signals, and ThreadMXBean
This article explains what a Java thread dump is, describes multiple ways to generate it—including JDK command‑line tools like jstack and jcmd, OS signals such as kill ‑3, and programmatic access via ThreadMXBean—provides syntax, options, examples, and recommendations for choosing the appropriate method.
Thread dump is a snapshot of all thread states in a Java process, showing stack traces for diagnosing issues.
JDK provides utilities located in the bin directory, such as jstack and jcmd , which can be invoked from the command line.
jstack
jstack captures a thread dump for a given PID and prints it to the console or a file.
Basic syntax:
jstack [-l][-e] <pid>Options:
-l : long format, prints additional lock information.
-e : extended format, prints extra thread information.
-?, -h, --help, -help : display help.
Example:
jstack 17264 > /tmp/threaddump.txtjcmd
jcmd is a versatile command‑line tool that sends commands to a JVM.
Basic syntax:
jcmd <pid|main class> <command ...>
jcmd -l
jcmd -hCommon options include -l to list JVM processes, -?, -h, --help for help, and many JVM‑specific commands such as Thread.print , GC.heap_dump , etc.
Example:
jcmd 17264 Thread.printOperating‑system signal
On Unix/Linux, sending kill -3 <pid> triggers a thread dump.
Redirecting output via JVM options
Use diagnostic VM options to write thread dumps to a file, e.g.:
-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=~/jvm.logProgrammatic generation with ThreadMXBean
The JMX ThreadMXBean can be used to dump all threads programmatically.
private static String threadDump(boolean lockedMonitors, boolean lockedSynchronizers) {
StringBuffer threadDump = new StringBuffer(System.lineSeparator());
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
for (ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) {
threadDump.append(threadInfo.toString());
}
return threadDump.toString();
}The method initializes a StringBuffer , obtains the ThreadMXBean via ManagementFactory , and calls dumpAllThreads() with optional lock information.
Graphical tools
Tools such as Java Mission Control (JMC), jvisualvm, and jconsole provide visual analysis of thread dumps.
Conclusion
The article lists several ways to capture Java thread dumps: JDK command‑line tools (jstack, jcmd, JMC, jvisualvm, jconsole), OS signals (kill ‑3), and programmatic access via ThreadMXBean . It recommends using jcmd on Java 8+ for full functionality, graphical tools for visual analysis, and OS or programmatic methods in production environments without JDK tools.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.