jstack - Stack Tracer to Generate Thread Dump

This section provides a tutorial example on how to use the 'jstack' tool to generate a thread dump (print all running threads and their stack traces).

What Is "jstack"? "jstack" is a JVM troubleshooting tool that generates a thread dump (prints stack traces of all running threads) of a given JVM process, a Java core file, or remote debug server. The "jstack" tool supports several functions as shown the this help message:

C:\herong>jstack -help

Usage:
    jstack [-l] <pid>
        (to connect to running process)

Options:
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

In order to test "jstack", I used this simple Java program, LongSleep.java:

/* LongSleep.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
class LongSleep {
   public static void main(String[] a) {
      Runtime rt = Runtime.getRuntime();
      System.out.println(" Free memory: " + rt.freeMemory());
      System.out.println("Total memory: " + rt.totalMemory());
      try {Thread.sleep(1000*60*60);} 
      catch (InterruptedException e) {}
   }
}

When LongSleep.java is running, I used "jps" to get its JVM process ID, pid. Then I ran "jstack" with that pid to generate a thread dump:

C:\herong>javac LongSleep.java

C:\herong>java LongSleep
 Free memory: 61276680
Total memory: 62914560

(Start another command window.)
C:\herong>jps -l -m
3296 LongSleep
2224 jdk.jcmd/sun.tools.jps.Jps -l -m

(Generate thread dump)
C:\herong>jstack 3296
Full thread dump Java HotSpot(TM) 64-Bit Server VM (10.0.1+10 mixed mode):

Threads class SMR info:
_java_thread_list=0x0000000023edc100, length=11, elements={...}

"main" #1 prio=5 os_prio=0 tid=0x0000000000231800 
   nid=0x3d1c waiting on condition  [0x000000000211f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(java.base@10.0.1/Native Method)
	at LongSleep.main(LongSleep.java:9)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001fb5b800 
   nid=0x2cec waiting on condition  [0x00000000235af000]
   java.lang.Thread.State: RUNNABLE
    at java.lang.ref.Reference.waitForReferencePendingList
       (java.base@10.0.1/Native Method)

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001fb5f800 
   nid=0x13f8 in Object.wait()  [0x000000002375e000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(java.base@10.0.1/Native Method)
	- waiting on <0x00000000c7b09410> 
       (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove
       (java.base@10.0.1/ReferenceQueue.java:151)
	- waiting to re-lock in wait() <0x00000000c7b09410> 
       (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove
       (java.base@10.0.1/ReferenceQueue.java:172)

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000023156000 
   nid=0x176c runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000023159000 
   nid=0x1d58 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000002315a000 
   nid=0x2970 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x0000000023162000 
   nid=0x2f7c waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000002316b000 
   nid=0x3380 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"Sweeper thread" #9 daemon prio=9 os_prio=2 tid=0x000000002318d800 
   nid=0x3d0c runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000023edb800 
   nid=0x1a2c runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #11 daemon prio=8 os_prio=1 tid=0x0000000023ee1000 
   nid=0x41d0 in Object.wait()  [0x000000002468f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
	at java.lang.Object.wait(java.base@10.0.1/Native Method)
	- waiting on <0x00000000c7a3ea58> 
       (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove
       (java.base@10.0.1/ReferenceQueue.java:151)
	- waiting to re-lock in wait() <0x00000000c7a3ea58> 
       (a java.lang.ref.ReferenceQueue$Lock)

"VM Thread" os_prio=2 tid=0x000000001fb56000 nid=0x34a0 runnable  

"GC Thread#0" os_prio=2 tid=0x0000000000249000 nid=0x3c80 runnable  

"GC Thread#1" os_prio=2 tid=0x000000000024a800 nid=0x4164 runnable  

"GC Thread#2" os_prio=2 tid=0x000000000024c800 nid=0x39a0 runnable  

"GC Thread#3" os_prio=2 tid=0x0000000000252000 nid=0x4248 runnable  

"G1 Main Marker" os_prio=2 tid=0x0000000000287000 nid=0x2c18 runnable  

"G1 Conc#0" os_prio=2 tid=0x0000000000288800 nid=0x30f0 runnable  

"G1 Refine#0" os_prio=2 tid=0x000000001fa72800 nid=0x3bf4 runnable  

"G1 Refine#1" os_prio=2 tid=0x000000001fa73800 nid=0x2cc0 runnable  

"G1 Refine#2" os_prio=2 tid=0x000000001fa7a800 nid=0x2468 runnable  

"G1 Refine#3" os_prio=2 tid=0x000000001fa7e000 nid=0x3c28 runnable  

"G1 Young RemSet Sampling" os_prio=2 tid=0x000000001fa7f000 
  nid=0x413c runnable  
"VM Periodic Task Thread" os_prio=2 tid=0x0000000023ee1800 
  nid=0x16f0 waiting on condition  

JNI global references: 2

Cool. I got my first thread dump of a JVM process. Now I know how many threads are running inside the JVM, 8 of them. But my Java application, LongSleep, only runs under 1 thread named as "main", which is in a state called, TIMED_WAITING. This matches my expectation.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK

 jinfo - VM Option Value Checker

 jinfo - Changing HotSpot VM Option

jstack - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

 Java Thread Deadlock Demo Program

 jstack - Detecting Java Thread Deadlocks

 jmap - JVM Heap Dump Tool

 Printing Histogram of Java Object Heap

 jmap - Generating Heap Dump File

 jhat - Java Heap Analysis Tool

 jhat - Starting Web Server on a Heap Dump File

 Listing Instance Counts of All Classes

 Browsing Object Instance Values

 Object Query Language (OQL)

 Searching for Instances with OQL Statements

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB