JFR and heap analysis
What you will learn
Profiling gathers evidence about a running program. Java Flight Recorder (JFR) records selected events; a histogram counts instances by type; a heap dump supports deeper object graph analysis. Choose the smallest useful observation first.
- Record and read a synthetic JFR event.
- Interpret histogram and heap dump evidence.
- Use diagnostic commands only on an owned JVM.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted local classes and fixed synthetic data. Never run these diagnostics against another person’s process. The runner enforces a process deadline.
Find the current JVM identity
public class Main {
public static void main(String[] args) throws Exception { long pid = ProcessHandle.current().pid(); System.out.println(pid > 0); System.out.println(!java.lang.management.ManagementFactory.getRuntimeMXBean().getVmName().isBlank()); }}Expected output:
truetrueThe positive process ID identifies the current owned process. Avoid broad name matching.
A safe workflow
Start with a symptom and a reproducible workload. For an owned JVM, identify its exact PID; use jcmd PID VM.version, GC.heap_info or Thread.print as needed. GC.class_histogram can be high impact and can trigger a collection depending on options. GC.heap_dump writes a sensitive snapshot and can pause the process; avoid it unless authorized, disk-budgeted and necessary. A histogram reports shallow counts and bytes, not retained size. A heap dump can expose paths from roots and dominators: if every root path to B passes through A, A dominates B. Retained size estimates memory made collectible if that object ceases to be reachable, subject to the analyzer’s model.
Record only useful events
JFR is a JDK event-recording facility. Use a short recording, enable a named event, stop, dump to a private lab path, and close. jfr summary and jfr print --events bytemind.lab.Sample FILE inspect a recording; JDK Mission Control (JMC) is a separate graphical analyzer. Events can be disabled or sampled, so absence of an event is not proof of absence of a behavior. JFR can help correlate allocation, pauses and thread activity, but does not replace a heap graph. Never upload real recordings or heap dumps without review and authorization.
Interpret data in context
Compare snapshots from equivalent workload phases, include counts and units, and distinguish allocations per second from retained bytes. A high allocation rate with a stable live set can be churn rather than a leak. Rising RSS with a stable heap can indicate native memory, thread stacks or direct buffers. In the following example only a synthetic custom event is enabled. No real user data is captured.
Record a synthetic event
public class Main { @jdk.jfr.Name("bytemind.lab.Sample") static class Sample extends jdk.jfr.Event { int units; } public static void main(String[] args) throws Exception { var file = java.nio.file.Files.createTempFile("bytemind-event-", ".jfr"); try { try (var recording = new jdk.jfr.Recording()) { recording.enable("bytemind.lab.Sample").withoutStackTrace(); recording.start(); var e = new Sample(); e.units = 7; e.commit(); recording.stop(); recording.dump(file); } try (var reader = new jdk.jfr.consumer.RecordingFile(file)) { while (reader.hasMoreEvents()) { var e = reader.readEvent(); if (e.getEventType().getName().equals("bytemind.lab.Sample")) System.out.println(e.getInt("units")); } } } finally { java.nio.file.Files.deleteIfExists(file); } }}Expected output:
7Only the chosen custom event is asserted; timings, file size and event metadata are deliberately not fixed.
Calculate a synthetic histogram delta
public class Main {
public static void main(String[] args) throws Exception { long earlier = 120; long later = 150; System.out.println(later - earlier); System.out.println((later - earlier) * 64); }}Expected output:
301920With a hypothetical shallow instance size of 64 bytes, the shallow-byte increase is 1920. This is a calculation over invented observations, not an actual retained-size measurement.
Common mistakes
Do not attach to unrelated processes, force collections in production, assume event absence is proof, treat histogram bytes as retained size, or share sensitive diagnostics.
Practice
Given synthetic histogram counts 200 and 215 for one class, calculate the change in count and shallow bytes at a hypothetical 32 bytes per instance. Print 15 and 480.
Expected output:
15480Compare your answer with the manual practice criteria.
Official reading: JFR · RecordingFile · jcmd · Diagnostics · jfr. Java 25 execution remains unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JFR and heap analysis — knowledge check
Original memory and diagnostics questions.
Which tool reads JFR files from the command line?
What does a class histogram primarily report?
When should heap dumps be shared?
What if a JFR event is absent?
What does retained size attempt to estimate?