jstack
About
jstack is a JDK utility used to generate thread dumps of a Java process. A thread dump contains a snapshot of all the threads running in the Java Virtual Machine (JVM) at a given time. It is useful for diagnosing issues like deadlocks, high CPU usage, thread contention, or long-running threads.
Key Use Cases
Identifying thread contention and deadlocks.
Debugging threads stuck in infinite loops or blocking states.
Understanding CPU-bound threads or hung processes.
Steps to Use jstack on Mac
1. Find the Process ID (PID):
Open the terminal and use the
jpscommand to list all Java processes:
jpsOutput example:

Start the Java Application (here it is sample-print-service maven springboot application)

jps command output is below

Here, 83322 is the PID of our Springboot app.
2. Generate a Thread Dump:
Run the
jstackcommand with the PID to generate a thread dump:
jstack 83322 > thread-dump.txtThis saves the thread dump to a file named thread-dump.txt in the current directory.

3. Analyze the Thread Dump:
Open the
thread-dump.txtfile and look for:BLOCKEDorWAITINGstates indicating thread contention.Deadlock information, which is explicitly mentioned if detected.
Long-running threads consuming resources.
Last updated