Sub topic 2
Recording with Java Flight Recorder
Assume basic Java, command-line execution, threads, heaps, and stack traces. Java Flight Recorder (JFR) is a low-overhead event recorder built into the JVM. It captures CPU samples, allocations, garbage collection, locks, threads, and compilation without changing application code.
Record an application
Start a recording for a fixed period:
java -XX:StartFlightRecording=duration=60s,filename=app.jfr,settings=profile -jar app.jarFor a running JVM, find its process ID and record without restarting it:
jcmd <pid> JFR.start name=adhoc settings=profile
jcmd <pid> JFR.dump name=adhoc filename=/tmp/adhoc.jfr
jcmd <pid> JFR.stop name=adhocThe profile configuration collects more detail than the default configuration. Use it when investigating a specific problem, and choose an appropriate duration so the recording does not become unnecessarily large.
Explore the recording in JDK Mission Control
Open app.jfr in JDK Mission Control (JMC). Begin with the event overview and recording timeline. Check CPU load, garbage collection pauses, allocation rate, thread activity, and lock instances. Narrow the time range to the slow request or workload. A profile is useful only when its events overlap the problem you are studying.
Find CPU hot spots
In the CPU view, inspect execution samples and call trees. A hot method appears often in samples, either because it runs for a long time or because callers invoke it frequently. Follow the stack upward to understand who calls it. Optimize only after connecting the method to a measurable goal such as p95 latency or throughput.
Safepoint bias
Some sampling profilers take samples only when a thread reaches a JVM safepoint. A tight loop or native call can run for a long time without reaching one, so the profile may blame another method or make the loop appear absent. Prefer tools that use asynchronous sampling or JFR events when this bias could affect the conclusion.
Read flame graphs
A flame graph shows stack traces. The horizontal width represents the total sampled time, and the vertical direction represents call depth. Wide frames are candidates for investigation. The tallest frame is not automatically the slowest one. Look for wide, repeated paths and verify them with another measurement.
Allocation rate and GC pressure
JFR’s allocation views show how quickly objects are created and which stacks create them. High allocation can trigger frequent garbage collections even when the live heap remains small. Use GC pauses and allocation rate together. A heap dump answers a different question: which objects remain reachable and may indicate a leak.
> callout note > A high allocation rate is not proof of a memory leak. A leak requires objects to remain reachable longer than intended.
Exercise
What should you check before optimizing a method that appears wide in a flame graph?
Show answer
Confirm that its samples overlap the reported performance problem, identify its callers, and measure the result after one targeted change.