🚀 A complete developer-friendly guide and hands-on toolkit to analyze, troubleshoot, and optimize Java memory usage using heap dumps and modern profilers like JProfiler,
jmap, and Eclipse MAT.
- 🔍 Introduction
- 🛠️ Tools You’ll Need
- 📤 Generating a Heap Dump
- 🧪 Sample Java Code to Generate Heap Dump
- 📥 Analyzing Heap Dump with JProfiler
- 🔎 Understanding Key Concepts
- 📊 Jargon Explained with Examples
- 🧠 Retained Size vs Shallow Size
- 🚨 Identifying Memory Leaks
- 🌐 References & Official Docs
- 🏷️ Tags & Topics
- 🙌 Contributing
- 📢 Spread the Word
- 💻 Author
This repository is your go-to guide for understanding Java heap dumps, analyzing them using JProfiler, and diagnosing memory leaks or performance bottlenecks. Whether you're debugging a production crash or fine-tuning memory usage—this toolkit has you covered.
| Tool | Purpose | Link |
|---|---|---|
| 🧰 JProfiler | Professional-grade heap dump analysis | JProfiler |
| 🧪 jmap | JVM CLI tool to generate heap dumps | jmap Docs |
| 🔬 Eclipse MAT | Free memory analyzer for .hprof |
MAT Download |
| 🛠️ VisualVM | Lightweight profiling/debugging tool | VisualVM |
jmap -dump:format=b,file=heapdump.hprof <PID><PID>is the Java process ID.heapdump.hprofis the name of the output file.
import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.ManagementFactory;
public class HeapDumpExample {
public static void main(String[] args) throws Exception {
HotSpotDiagnosticMXBean mxBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
mxBean.dumpHeap("heapdump.hprof", true);
System.out.println("Heap dump created!");
}
}-
Open JProfiler
-
Go to
File → Open Snapshot -
Select your
.hproffile -
Start analyzing with:
Heap WalkerReference GraphDominator TreeGC RootsAllocation Call Tree
| View | Purpose |
|---|---|
| Classes View | Identify classes with max memory footprint |
| Instances View | Inspect each object instance and size |
| Reference Graph | Understand object relationships |
| GC Roots | Discover objects keeping others alive |
| Dominator Tree | Visualize memory retention hierarchy |
| Allocation Tree | Analyze object allocation paths |
| Term | Meaning |
|---|---|
| Heap | The memory area where Java objects live |
| Shallow Size | Memory directly consumed by the object |
| Retained Size | Total memory retained if this object and its exclusives were GC’d |
| GC Root | Root reference point in JVM memory graph |
| Dominator | Object whose deletion would make child objects unreachable |
| Reference Chain | Path from GC root to the object, useful for memory leak analysis |
Object A retains B and C
├── B
└── C
If B and C are only accessible through A,
then A's retained size = shallow size of A + B + C
- Shallow Size: Just A
- Retained Size: A + all exclusively retained objects
| Metric | What it Tells You | Example |
|---|---|---|
| Shallow Size | Object's own memory | 40 bytes |
| Retained Size | Memory freed if object + dependents removed | 40 + B (30) + C (20) |
- High retained sizes
- Excessive number of instances of a class
- Objects held by static fields, thread locals, or long reference chains
- Large collections (
Map,List) with old or unnecessary data
Use JProfiler features like:
- Leak Suspect Reports
- Dominator Tree
- Reference Graph
- Oracle JDK jmap
- Eclipse MAT Documentation
- JProfiler Overview
- Java Memory Leaks - Baeldung
- YouTube: Heap Dump Tutorials
#Java #HeapDump #JProfiler #MemoryLeak #GC #MAT #MemoryOptimization #JVM #ReferenceGraph #PerformanceTuningPull requests welcome! Feel free to add:
- Additional tools
- More case studies
- Tips for JVM tuning
- Alternative profiling tools (YourKit, VisualVM)
If this helped you:
- ⭐ Star the repo
- 🔁 Share with your team
- 📦 Fork and enhance
Crafted with 💙 by @Sharique55
