> 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/heap-dump-capture/jmap.md).

# jmap

## About

`jmap` is a JDK utility used to generate heap dumps or analyze memory usage in a Java application. A heap dump is a snapshot of the JVM's memory, including all objects and references at a specific point in time. It is essential for diagnosing memory leaks, `OutOfMemoryError`, or high memory usage.

## **Key Use Cases**

* Diagnosing memory leaks by analyzing objects and their references.
* Understanding the distribution of objects in memory.
* Debugging `OutOfMemoryError` issues.

## **Steps to Use jmap on Mac**

### **1. Find the Process ID (PID):**

* Use the `jps` command to locate your Spring Boot app’s PID:

```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. Capture a Heap Dump:**

* Run the following `jmap` command to generate a heap dump:

```bash
jmap -dump:format=b,file=heap-dump.hprof 83322
```

* `format=b`: Specifies the binary format for the dump file.
* `file=heap-dump.hprof`: Specifies the file name for the heap dump.
* The `heap-dump.hprof` file will be saved in the current directory.

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

{% file src="/files/5jdcyycSHEgpcQtQCSm2" %}

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

* Use tools like **Eclipse MAT (Memory Analyzer Tool)** or **VisualVM** to open and analyze the `heap-dump.hprof` file.
  * Look for objects with high retention, large collections, or unreferenced objects.
  * Detect memory leaks by identifying objects that cannot be garbage collected.

{% hint style="info" %}
&#x20;We can get a summary of memory usage without creating a heap dump with jmap supported options
{% endhint %}


---

# 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/heap-dump-capture/jmap.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.
