How to use the command 'jstack' (with examples)

How to use the command 'jstack' (with examples)

The ‘jstack’ command is a useful tool for troubleshooting and analyzing Java applications. It provides information about the Java stack traces for all or specific threads in a Java process, including mixed mode (Java/C++) stack traces. It can also be used to print stack traces from a Java core dump.

Use case 1: Print Java stack traces for all threads in a Java process

Code:

jstack java_pid

Motivation:

  • This use case is helpful when you want to examine the current stack traces for all threads in a Java process.
  • It can be used for troubleshooting performance issues, deadlock detection, or identifying bottlenecks in the application.

Explanation:

  • ‘jstack’ is the command to execute.
  • ‘java_pid’ is the process ID (PID) of the Java process you want to inspect.

Example Output:

Attaching to process ID <java_pid>, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 11.0.11+9-LTS
Full thread dump OpenJDK 64-Bit Server VM (11.0.11+9-LTS mixed mode):

"main" #1 prio=5 os_prio=0 cpu=4055.25ms elapsed=19.06s tid=0x00007f7392005800 nid=0x781d waiting on condition  [0x00007f73b6af6000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.Application.main(Application.java:10)
...

Use case 2: Print mixed mode (Java/C++) stack traces for all threads in a Java process

Code:

jstack -m java_pid

Motivation:

  • This use case is useful when you need to inspect both Java and native (C++) stack frames for each thread in a Java process.
  • It can help in detecting issues that involve interactions between Java and native code or understanding the performance characteristics of native code invocations.

Explanation:

  • ‘jstack’ is the command to execute.
  • ‘-m’ is the argument to enable mixed mode stack trace printing.
  • ‘java_pid’ is the process ID (PID) of the Java process you want to inspect.

Example Output:

Attaching to process ID <java_pid>, please wait...
Debugger attached successfully.
Server compiler detected.
Full thread dump OpenJDK 64-Bit Server VM (11.0.11+9-LTS mixed mode):

"main" #1 prio=5 os_prio=0 cpu=4055.25ms elapsed=19.06s tid=0x00007f7392005800 nid=0x781d runnable  [0x00007f73b6af6000]
   java.lang.Thread.State: RUNNABLE
        at com.example.MyNativeMethod(Native Method)
        at com.example.Application.main(Application.java:10)
...

Use case 3: Print stack traces from Java core dump

Code:

jstack /usr/bin/java file.core

Motivation:

  • This use case is helpful when you have a Java core dump file and you want to analyze it for issues or understand the state of the application at the time of the crash.
  • It allows you to retrieve stack traces from the core dump and analyze the state of the threads.

Explanation:

  • ‘jstack’ is the command to execute.
  • ‘/usr/bin/java’ is the Java executable path.
  • ‘file.core’ is the path to the Java core dump file you want to analyze.

Example Output:

Attaching to core file /usr/bin/java from executable /usr/bin/java, please wait...
Debugger attached successfully.
Server compiler detected.
Full thread dump OpenJDK 64-Bit Server VM (11.0.11+9-LTS mixed mode):

"main" #1 prio=5 os_prio=0 cpu=4055.25ms elapsed=19.06s tid=0x00007f7392005800 nid=0x781d waiting on condition  [0x00007f73b6af6000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.Application.main(Application.java:10)
...

Conclusion:

The ‘jstack’ command is a versatile and powerful tool for analyzing Java applications. By providing stack traces for threads, including mixed mode stack traces and the ability to analyze core dump files, it helps in diagnosing performance issues, debugging problems, and understanding the behavior of a Java process at a specific point in time.

Related Posts

Using the `arch` Command (with examples)

Using the `arch` Command (with examples)

The arch command in Linux is used to display the name of the system architecture.

Read More
How to use the command st-flash (with examples)

How to use the command st-flash (with examples)

The st-flash command is a utility for flashing binary files to STM32 ARM Cortex microcontrollers.

Read More
How to use the command 'retry' (with examples)

How to use the command 'retry' (with examples)

The retry command is used to repeat a command until it succeeds or until a specific criterion is met.

Read More