Sub topic 13
Diagnosing with Heap Dumps, jcmd, and Java Flight Recorder
Assume you can run Java programs from a command line and understand objects, references, and the JVM heap. This chapter shows how to investigate high memory use, frequent garbage collection, and OutOfMemoryError in Java 11.
Start with the symptom
A memory problem can have several causes:
- The heap is too small for a legitimate workload.
- A collection, cache, or listener keeps objects reachable.
- Metaspace grows because applications or class loaders are not released.
- The process spends too much time collecting garbage.
- Native memory outside the Java heap is exhausted.
First identify the process and observe it before changing options. The process ID, or PID, is the number assigned to the running Java process.
jcmd -lThis lists Java processes that the current user can inspect. Use the PID with diagnostic commands.
Inspect the live heap with `jcmd`
jcmd is included with the JDK. The GC.heap_info command gives a short summary of the heap and collector:
jcmd 12345 GC.heap_infoReplace 12345 with the actual PID. For a class-level view, use a class histogram:
jcmd 12345 GC.class_histogramThe histogram lists classes and the approximate number of instances and bytes they occupy. Run it twice during the same workload. A class whose count keeps increasing may point to a retained collection, cache, or queue.
A histogram is evidence, not proof of a leak. Some objects are expected to grow while a request is being processed. Compare the result with the application's normal steady state.
Capture and inspect a heap dump
A heap dump is a snapshot of objects in the Java heap. It records objects, their sizes, and references between them. Capture one when memory use is high:
jcmd 12345 GC.heap_dump /tmp/app-heap.hprofThe command can cause a long pause while the JVM creates the dump. It also writes a large file, so check available disk space first. A dump may contain passwords, tokens, personal data, and request contents. Protect it like production data.
Open the .hprof file with a heap-analysis tool such as Eclipse Memory Analyzer. Look for:
- Dominator objects, which retain large portions of the heap.
- Large collections and their retained elements.
- Paths from GC roots to suspicious objects.
- Multiple class loaders holding application classes.
- Duplicate strings or unexpectedly large byte arrays.
A GC root is a JVM-managed starting point for reachability, such as a live thread, static field, or JNI reference. If a suspicious object has a path from a GC root, the collector must preserve it.
Record runtime behavior with Java Flight Recorder
Java Flight Recorder, or JFR, records JVM and application events with low overhead. It helps connect allocation, garbage collection, CPU activity, locks, and pauses over time.
Start a short recording on a running Java 11 process:
jcmd 12345 JFR.start name=memory duration=60s filename=/tmp/memory.jfr settings=profileCheck active recordings and dump one manually if needed:
jcmd 12345 JFR.check
jcmd 12345 JFR.dump name=memory filename=/tmp/memory.jfrOpen the recording in Java Mission Control. Examine garbage-collection pauses, heap occupancy, allocation rates, object promotion, and threads that allocate heavily. A high allocation rate can cause frequent young collections even when no leak exists. A steadily rising old generation is more suspicious.
| Tool | Best question it answers | Main cost |
|---|---|---|
| `GC.heap_info` | What is the heap and collector doing now? | Small |
| Class histogram | Which classes occupy the heap? | Moderate |
| Heap dump | What references retain these objects? | Large pause and disk use |
| JFR | When and why do allocations and pauses occur? | Recording storage and analysis |
A practical investigation sequence
- Record the symptom: heap usage, pause times, error message, and workload.
- Run
jcmd -land inspectGC.heap_info. - Compare class histograms at two points in time.
- Capture a heap dump when the suspected retention is present.
- Use GC-root paths and dominators to find the retaining reference.
- Use JFR to correlate allocation and garbage-collection behavior.
- Fix the owner or lifecycle of the retained objects, then repeat the measurement.
Exercise
A class histogram shows that byte[] instances grow continuously, but the heap dump shows they are retained by a static Map. What should you investigate first?
Show answer
Investigate the map's eviction and lifecycle rules. A static map is reachable from a GC root, so its entries remain collectible only when the map removes them or the static reference is cleared.