Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.11

'jstack' - Stack Tracer of JVM Threads

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

"jstack": A JVM troubleshooting tool that 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 1.6 Windows version only supports limited functions as shown the this help message:

C:\herong>\Progra~1\java\jdk1.6.0_02\bin\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) 2008 by Dr. Herong Yang, http://www.herongyang.com/
 */
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 get the following stack information:

C:\herong>\Progra~1\java\jdk1.6.0_02\bin\javac LongSleep.java

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


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


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

Full thread dump Java HotSpot(TM) Client VM (1.6.0_02-b06 mixed mod...

"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. Now I know how many threads are running inside a 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.

Sections in This Chapter

JVM Troubleshooting Tools in JDK 1.5

'jinfo' - VM Option Value Checker

Changing HotSpot VM Option using 'jinfo'

'jstack' - Stack Tracer of JVM Threads

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

Dr. Herong Yang, updated in 2008
'jstack' - Stack Tracer of JVM Threads