> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-development-tools/thread-dump-capture/jstack.md).

# 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 `jps` command to list all Java processes:

```bash
jps
```

Output example:

<figure><img src="/files/Nn6179bpbIcu5oJymtuD" alt="" width="369"><figcaption></figcaption></figure>

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

<figure><img src="/files/UtvzQivrLhYJHwbVCqo5" alt="" width="563"><figcaption></figcaption></figure>

`jps` command output is below

<figure><img src="/files/zZ3Pm9GRfQTczNBMBPZ9" alt="" width="375"><figcaption></figcaption></figure>

Here, `83322` is the PID of our Springboot app.

### **2. Generate a Thread Dump:**

* Run the `jstack` command with the PID to generate a thread dump:

```bash
jstack 83322 > thread-dump.txt
```

This saves the thread dump to a file named `thread-dump.txt` in the current directory.

<figure><img src="/files/ZKlhPvvQ18EYWLFSQa8u" alt="" width="563"><figcaption></figcaption></figure>

{% file src="/files/vWjGAgmKjkLQ9wKygid2" %}

### **3. Analyze the Thread Dump:**

* Open the `thread-dump.txt` file and look for:
  * `BLOCKED` or `WAITING` states indicating thread contention.
  * Deadlock information, which is explicitly mentioned if detected.
  * Long-running threads consuming resources.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-development-tools/thread-dump-capture/jstack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
