MAT Heap Dump Analysis
Analyze Java heap dumps headlessly using Eclipse MAT via the scripts/mat.sh shell wrapper.
Prerequisites
- MAT installed at
/Applications/MemoryAnalyzer.app/Contents/Eclipse/(new) or/Applications/mat.app/Contents/Eclipse/(legacy), auto-detected in that order (or setMAT_HOME) - Java 17+ available via
$JAVA_HOME/bin/java,/usr/libexec/java_home -V, orjavaon PATH
Script Location
scripts/mat.sh in this skills folder.
Environment Variables
| Variable | Default | Description |
|---|---|---|
MAT_HOME |
auto-detected (MemoryAnalyzer.app → mat.app) |
MAT installation root |
MAT_XMX |
4g |
Java max heap size for MAT |
MAT_TIMEOUT |
1800 |
Command timeout in seconds |
JAVA_HOME |
(system) | Java installation directory |
Operations
1. Healthcheck — Verify MAT + Java setup
scripts/mat.sh healthcheck
Run this first to confirm MAT and Java are available.
2. Index Status — Check if a heap dump is already indexed
scripts/mat.sh index-status /path/to/dump.hprof
If the heap dump has been parsed before, MAT creates .index files next to it. This avoids re-indexing on subsequent operations.
3. Parse Report — Run predefined analysis reports
scripts/mat.sh report /path/to/dump.hprof <report_id> [options...]
Available report IDs:
| Report ID | Description |
|---|---|
org.eclipse.mat.api:suspects |
Leak suspects report — identifies likely memory leaks |
org.eclipse.mat.api:overview |
Heap overview — class histogram, top consumers, system info |
org.eclipse.mat.api:top_components |
Top memory-consuming components |
org.eclipse.mat.api:compare |
Comparison report (requires -baseline /path/to/other.hprof) |
org.eclipse.mat.api:suspects2 |
Leak suspects comparison |
org.eclipse.mat.api:overview2 |
Overview comparison |
Examples:
# Leak suspects
scripts/mat.sh report dump.hprof org.eclipse.mat.api:suspects
# Overview
scripts/mat.sh report dump.hprof org.eclipse.mat.api:overview
# Comparison (two heap dumps)
scripts/mat.sh report dump.hprof org.eclipse.mat.api:compare -baseline baseline.hprof
Reports generate HTML/ZIP artifacts in the same directory as the heap dump.
4. OQL Query — Execute Object Query Language queries
scripts/mat.sh oql /path/to/dump.hprof "<query>" [--format txt|html|csv] [--limit N]
Examples:
# List all String objects
scripts/mat.sh oql dump.hprof "SELECT * FROM java.lang.String" --limit 100
# Inspect fields of a specific object by address
scripts/mat.sh oql dump.hprof "SELECT p.topic FROM OBJECTS 0x12345678 p"
# Find instances of a class
scripts/mat.sh oql dump.hprof "SELECT p FROM INSTANCEOF com.example.MyClass p" --limit 50
5. Run Command — Execute built-in MAT analysis commands
scripts/mat.sh command /path/to/dump.hprof <command_name> [args] [--format txt|html|csv] [--limit N]
Examples:
# Class histogram
scripts/mat.sh command dump.hprof histogram
# Dominator tree
scripts/mat.sh command dump.hprof dominator_tree
# Path to GC roots for a specific object
scripts/mat.sh command dump.hprof path2gc 0x12345678
# Thread overview
scripts/mat.sh command dump.hprof thread_overview
# Find strings
scripts/mat.sh command dump.hprof find_strings --limit 200
Supported Commands (56 total)
See references/commands.md for the full list with descriptions.
OQL Specification
MAT uses its own OQL dialect in "parse-application command mode":
Command format: -command=oql "<query>"
Supported Patterns
| Pattern | Example | Use Case |
|---|---|---|
| Instance scan | SELECT p FROM INSTANCEOF com.example.MyClass p |
Iterate objects by class |
| Field extraction | SELECT p.topic FROM OBJECTS 0x12345678 p |
Read fields from a specific object |
| Boolean/state check | SELECT p.isClosing FROM OBJECTS 0x12345678 p |
Validate lifecycle flags |
| Class histogram | Use org.eclipse.mat.api:overview report instead |
Ranking classes by count/size |
Known Limitations
- SQL-like
GROUP BY/ORDER BYfrequently fail in parse-app query mode - Dialect-specific helpers like
classof(...)may be rejected depending on parser context - For ranking/top classes, prefer MAT overview/suspects reports then parse the artifacts
- For root-cause inspection, use targeted field-level OQL against object addresses
Instructions for Heap Dump Analysis
Recommended Workflow
- Healthcheck first: Run
scripts/mat.sh healthcheckto verify the setup - Check index status: Run
scripts/mat.sh index-status <heap>— if already indexed, subsequent operations will be faster - Start with overview: Run
scripts/mat.sh report <heap> org.eclipse.mat.api:overviewto get class histogram and system info - Run leak suspects: Run
scripts/mat.sh report <heap> org.eclipse.mat.api:suspectsfor automatic leak detection - Drill down: Use
scripts/mat.sh command <heap> histogramfor class-level breakdown, thendominator_treefor retained size analysis - Investigate specific objects: Use
scripts/mat.sh oql <heap> "SELECT ..."to inspect individual objects by address or class - Thread analysis: Use
scripts/mat.sh command <heap> thread_overviewto see thread stacks and local variables
Reading Results
- Reports generate HTML directories and ZIP files next to the heap dump (e.g.,
dump_Leak_Suspects/) - Queries and commands produce text results in
*_Query/pages/Query_Command*.txt - The script automatically displays text results and lists generated artifacts
- For HTML reports, read the generated files in the report directory
Tips
- First-time analysis of a heap dump triggers indexing, which can take minutes for large dumps. Subsequent operations reuse the index.
- Increase
MAT_XMXfor very large heap dumps (e.g.,MAT_XMX=8g) - Increase
MAT_TIMEOUTfor long-running analyses (e.g.,MAT_TIMEOUT=3600) - Use
--limitwith OQL and commands to avoid overwhelming output - The
path2gccommand is invaluable for understanding why objects are retained - Compare two heap dumps with
org.eclipse.mat.api:compareto find growth patterns