'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).

"jstack": 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 included in the JDK Windows version only supports limited functions as shown the this help message:

C:\herong>\Progra~1\java\jdk1.8.0\bin\jstack -help

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

    -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>\Progra~1\java\jdk1.8.0\bin\javac LongSleep.java

C:\herong>\Progra~1\java\jdk1.8.0\bin\java LongSleep
 Free memory: 4997104
Total memory: 5177344

(Start another command window.)
C:\herong>\Progra~1\java\jdk1.8.0\bin\jps -l -m
3296 LongSleep
2224 sun.tools.jps.Jps -l -m

C:\herong>\Progra~1\java\jdk1.8.0\bin\jstack 3296

Full thread dump Java HotSpot(TM) Client VM (1.8.0 ...

"Low Memory Detector" daemon prio=6 tid=0x02a7c800 nid=0xf20 runnable
   java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x02a78000 nid=0xb3c waiting ...
   java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x02a76c00 nid=0x37c waiting ...
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x02a75c00 nid=0xd7c runnable 
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x02a71400 nid=0x2e8 in Object.wait() 
   java.lang.Thread.State: WAITING (on object monitor)
   at java.lang.Object.wait(Native Method)
   - waiting on <0x22990b38> (a java.lang.ref.ReferenceQueue$Lock)
   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:116)
   - locked <0x22990b38> (a java.lang.ref.ReferenceQueue$Lock)
   at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:132)
   at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=0x02a6d000 nid=0xfbc in Obje...
   java.lang.Thread.State: WAITING (on object monitor)
   at java.lang.Object.wait(Native Method)
   - waiting on <0x22990a38> (a java.lang.ref.Reference$Lock)
   at java.lang.Object.wait(Object.java:485)
   at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
   - locked <0x22990a38> (a java.lang.ref.Reference$Lock)

"main" prio=6 tid=0x00296000 nid=0xef4 waiting on condition [0x0090...
   java.lang.Thread.State: TIMED_WAITING (sleeping)
   at java.lang.Thread.sleep(Native Method)
   at LongSleep.main(LongSleep.java:10)

"VM Thread" prio=10 tid=0x02a63c00 nid=0xc70 runnable

"VM Periodic Task Thread" prio=10 tid=0x02a7e000 nid=0xdd0 waiting ...

JNI global references: 571

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: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK 1.5

 'jinfo' - VM Option Value Checker

 Changing HotSpot VM Option using 'jinfo'

'jstack' - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

 Java Thread Deadlock Demo Program

 Detecting Java Thread Deadlocks with 'jstack'

 'jmap' - JVM Heap Dump Tool

 Printing Histogram of Java Object Heap

 Generating Heap Dump File with 'jmap'

 'jhat' - Java Heap Analysis Tool

 Starting 'jhat' 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

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials


 PDF Printing Version