π 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.hprof
is 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
.hprof
file -
Start analyzing with:
Heap Walker
Reference Graph
Dominator Tree
GC Roots
Allocation 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 #PerformanceTuning
Pull 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