# 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="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2F2XHtlRnnE6LjemspfBnr%2Fjstack-1.png?alt=media&#x26;token=072ceb8d-e6d2-4ecc-8bf2-3756be9c959d" alt="" width="369"><figcaption></figcaption></figure>

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

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2F9Y3hMul4YloUNZjMywbg%2Fjstack-2.png?alt=media&#x26;token=073748fa-a66c-4de8-8d4c-83b8b70b8389" alt="" width="563"><figcaption></figcaption></figure>

`jps` command output is below

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2FW8QNCCZIRKfUPB9nwD0f%2Fjstack-3.png?alt=media&#x26;token=9bedb4aa-41cd-4580-b688-954ef29d38eb" 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="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2FpPtDYgTIMztlWOtf9c7G%2Fjmap-2.png?alt=media&#x26;token=8251aacf-8a75-4841-a391-cfebafeab567" alt="" width="563"><figcaption></figcaption></figure>

{% file src="<https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2FJplAWYz2sIgBUTsz0Tka%2Fheap-dump.hprof?alt=media&token=5a74dd92-7d80-469c-93f5-80f843fd0c2d>" %}

### **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 %}
