|
| 1 | +--- |
| 2 | +name: mat-cli |
| 3 | +description: Investigate Java heap dumps with Eclipse MAT CLI. Use when Codex needs to triage `.hprof` or `.phd` dumps, find dominant retainers or exploding classes, inspect object fields or nested values, trace paths to GC roots, review heap-derived thread state, run MAT reports, or escalate to OQL for targeted heap forensics. |
| 4 | +--- |
| 5 | + |
| 6 | +# Heap Dump Analysis with mat-cli |
| 7 | + |
| 8 | +Use `mat-cli` as the first-line tool for Java heap-dump triage. Prefer the dedicated CLI commands with stable JSON contracts, then escalate to `query` or `oql` only when a direct command cannot answer the question cleanly. |
| 9 | + |
| 10 | +If `mat-cli` is unavailable, install it with `brew install demogorgon314/mat-cli/mat-cli`. Inside the MAT source tree, you can also build it from `parent/` with `mvn clean package -DskipTests -Dmat-product=mat-cli`. |
| 11 | + |
| 12 | +## Quick start |
| 13 | + |
| 14 | +- Confirm `mat-cli` is reachable with `mat-cli --help`. |
| 15 | +- Prefer `--format json` while reasoning, then rerun one or two focused commands with `--format text` when you need readable excerpts for the user. |
| 16 | +- Use absolute heap paths and keep every suspect object's `0x...` address in your notes. |
| 17 | +- Keep companion files beside the heap when possible. For OpenJ9 `.phd` dumps, a nearby `javacore` can improve thread analysis. |
| 18 | +- Use `--query-file` or `--command-file` for long expressions, embedded quotes, regexes, baseline paths, or inner class names containing `$`. |
| 19 | + |
| 20 | +```bash |
| 21 | +mat-cli summary <heap> --format json |
| 22 | +mat-cli biggest-objects <heap> --format json --limit 20 --depth 3 |
| 23 | +mat-cli objects <heap> --by class --format json --limit 30 |
| 24 | +mat-cli instances <heap> --class com.example.CacheEntry --format json --limit 20 |
| 25 | +mat-cli inspect-object <heap> --object 0x1234abcd --field-paths value --format json |
| 26 | +mat-cli path2gc <heap> --object 0x1234abcd --format json --depth 8 --limit 20 |
| 27 | +``` |
| 28 | + |
| 29 | +If the leak might be thread-local, blocked, or finalizer-related, insert `mat-cli threads <heap> --format json --limit 20` right after `summary`. |
| 30 | + |
| 31 | +## Command map |
| 32 | + |
| 33 | +### Baseline |
| 34 | + |
| 35 | +```bash |
| 36 | +mat-cli summary <heap> --format json |
| 37 | +mat-cli threads <heap> --format json --limit 20 |
| 38 | +``` |
| 39 | + |
| 40 | +Use `summary` for heap format, object count, class count, and used heap. Use `threads` early when the leak might be thread-local, blocked, or finalizer-related. Thread output is best-effort heap metadata, not a live `jstack`. |
| 41 | + |
| 42 | +### Dominators |
| 43 | + |
| 44 | +```bash |
| 45 | +mat-cli biggest-objects <heap> --format json --limit 20 --depth 3 |
| 46 | +mat-cli objects <heap> --by class --format json --limit 30 |
| 47 | +mat-cli objects <heap> --by package --format json --limit 20 |
| 48 | +mat-cli objects <heap> --by class-loader --format json --limit 20 |
| 49 | +``` |
| 50 | + |
| 51 | +Use `biggest-objects` to find the top retained dominators. Use `objects --by class` for class growth, `objects --by package` for retained package trees, and `objects --by class-loader` for class-loader ownership. Keep `_address` values from `biggest-objects` rows for follow-up inspection. |
| 52 | + |
| 53 | +### Class drilldown |
| 54 | + |
| 55 | +```bash |
| 56 | +mat-cli instances <heap> --class com.example.CacheEntry --format json --limit 20 |
| 57 | +mat-cli instances <heap> --class-regex 'com\\.example\\..*Cache.*' --format json --limit 20 |
| 58 | +mat-cli instances <heap> --class-contains ThreadLocal --include-subclasses --format json --limit 30 |
| 59 | +``` |
| 60 | + |
| 61 | +Use `instances` when the user names a class or when `objects --by class` points at one. Prefer `--class` for exact matches. Use regex or contains matching only when the package or suffix is uncertain. Add `--include-subclasses` only when inheritance matters. |
| 62 | + |
| 63 | +### Object inspection |
| 64 | + |
| 65 | +```bash |
| 66 | +mat-cli inspect-object <heap> --object 0x1234abcd --format json --depth 4 --limit 20 |
| 67 | +mat-cli inspect-object <heap> --object 0x1234abcd --select-fields value --format json --limit 20 |
| 68 | +mat-cli inspect-object <heap> --object 0x1234abcd --field-paths cleaner.offsetMap --format json |
| 69 | +mat-cli inspect-object <heap> --object 0x1234abcd --format text --field-paths count |
| 70 | +``` |
| 71 | + |
| 72 | +Use `inspect-object` when you need concrete field values from one object. Reach for `--select-fields` when direct fields are the payload, `--field-paths` for one or more nested values, and `--show-nulls` only when null references are part of the bug story. Increase `--depth` carefully; the default is intentionally conservative. |
| 73 | + |
| 74 | +### Retention |
| 75 | + |
| 76 | +```bash |
| 77 | +mat-cli path2gc <heap> --object 0x1234abcd --format json --depth 8 --limit 20 |
| 78 | +mat-cli query <heap> --command "show_dominator_tree 0x1234abcd" --format json --limit 20 --depth 4 |
| 79 | +mat-cli query <heap> --command "merge_shortest_paths -groupby FROM_GC_ROOTS com.example.CacheEntry" --format json --limit 20 --depth 4 |
| 80 | +``` |
| 81 | + |
| 82 | +Use `path2gc` for the shortest retaining path to GC roots. Use `show_dominator_tree` when `path2gc` is too narrow and you need local dominator context. Use `merge_shortest_paths` when many leaking instances appear to flow through one shared retaining structure. If `path2gc` says the object is already a GC root, say that explicitly and pivot back to `inspect-object`, `threads`, or a dominator query. |
| 83 | + |
| 84 | +### Advanced MAT queries |
| 85 | + |
| 86 | +```bash |
| 87 | +mat-cli query <heap> --command "thread_overview" --format json |
| 88 | +mat-cli query <heap> --command "finalizer_thread" --format json |
| 89 | +mat-cli query <heap> --command "default_report org.eclipse.mat.api:suspects" --format json |
| 90 | +mat-cli query <heap> --command "default_report org.eclipse.mat.api:overview2 -params baseline=/abs/path/baseline.hprof" --format json |
| 91 | +mat-cli oql <heap> --query-file suspects.oql --format json --limit 20 |
| 92 | +``` |
| 93 | + |
| 94 | +Use `query` for registered MAT reports and analyses that do not have dedicated CLI wrappers. Use `oql` only after `objects`, `biggest-objects`, `instances`, `inspect-object`, or `path2gc` stop being expressive enough. Prefer `--command-file` or `--query-file` when quoting gets awkward. |
| 95 | + |
| 96 | +## Command discovery |
| 97 | + |
| 98 | +```bash |
| 99 | +mat-cli describe summary --format json |
| 100 | +mat-cli describe objects --format json |
| 101 | +mat-cli schema inspect-object --format json |
| 102 | +mat-cli list-queries --format json |
| 103 | +mat-cli describe-query histogram --format json |
| 104 | +``` |
| 105 | + |
| 106 | +Use `describe` and `schema` before scripting against a CLI command's JSON output. Use `list-queries` and `describe-query` before reaching for less familiar MAT query ids or arguments. `histogram` is still useful as a MAT query id, but it is no longer the preferred first-line CLI command for class aggregation. |
| 107 | + |
| 108 | +## Reporting rules |
| 109 | + |
| 110 | +- Separate shallow heap from retained heap. |
| 111 | +- Quote exact class names and object addresses. |
| 112 | +- Treat `objects --by class` and `objects --by class-loader` retained sizes as approximate unless the command guarantees otherwise. |
| 113 | +- Tie each leak suspect to at least two pieces of evidence, such as a `biggest-objects` row, an `objects` view, a `path2gc` chain, a field value, a thread entry, or a MAT report section. |
| 114 | +- Call out uncertainty clearly when stack frames are missing or when the dump only offers best-effort thread metadata. |
| 115 | +- Prefer a short evidence chain over dumping raw JSON or huge tables back to the user. |
| 116 | + |
| 117 | +## Specific tasks |
| 118 | + |
| 119 | +- Ready-to-run command sequences and advanced MAT query patterns: [references/command-playbook.md](references/command-playbook.md) |
| 120 | +- Validated OQL snippets when direct commands are not enough: [references/oql-recipes.md](references/oql-recipes.md) |
0 commit comments