> 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/troubleshooting-java-code/heap-dump.md). Agents can also search and read this documentation through its [MCP server](https://www.pranaypourkar.co.in/the-programmers-guide/~gitbook/mcp).

# Heap Dump

## About

A **heap dump** is a snapshot of the heap memory of the JVM at a specific point in time. It contains information about:

* All objects in memory
* Their references and relationships
* Primitive values stored in those objects

## Why is it Useful?

* **Memory leaks**: Identify objects that are occupying excessive memory and are not being garbage collected.
* **OutOfMemoryError**: Helps debug what caused the application to run out of memory.
* **Object reference issues**: Analyze how objects are related to each other.

## When to Capture a Heap Dump?

* When the application experiences **OutOfMemoryError**.
* To analyze memory consumption trends.
* To identify **unreachable but still-referenced objects**.

## How to Generate a Heap Dump?

### **Automatically on OutOfMemoryError**&#x20;

Add this JVM option to enable heap dump generation on `OutOfMemoryError`:

```bash
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<path-to-save-dump>
```

### **Using JDK Tools**

`jmap -dump:live,format=b,file=<file-name>.hprof <pid>`: Generates a heap dump.

### **Using Tools**

Tools like Eclipse MAT (Memory Analyzer Tool), VisualVM, JConsole, or IntelliJ IDEA.

## Example Output

Heap dumps are binary files (e.g., `heapdump.hprof`) and cannot be read directly. We must use tools like Eclipse MAT or VisualVM to analyze them.

## How to Analyze a Heap Dump?

* **Look for retained memory**: Identify objects that consume the most memory.
* **Inspect object references**: Trace how objects reference each other.
* **Analyze garbage collection**: Check for objects that should have been collected but are not.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/troubleshooting-java-code/heap-dump.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
