Sub topic 6
Recording with Java Flight Recorder
This chapter assumes you can run Java from the command line, read stack traces, and know what the heap and threads are. It also assumes you have a JDK 11 or later, where Java Flight Recorder (JFR) is built in and free to use.
What JFR is
JFR is an event recorder inside the JVM. The JVM and the JDK libraries emit small timestamped events: method samples, allocations, garbage collections, lock waits, file and socket I/O. JFR writes them to a binary .jfr file. Because it runs inside the JVM and buffers events in memory, its overhead is low. The default settings typically cost around 1% or less, which is why teams use it on live systems.
Start a recording at launch
Use this when you can restart the application, or when you want to capture startup.
java -XX:StartFlightRecording=duration=60s,filename=app.jfr,settings=profile -jar app.jarThe options are:
duration=60sstops the recording after 60 seconds.filename=app.jfris where the file is written when the recording ends.settings=profileselects a settings file.defaultis lighter.profilesamples Java stacks more often and enables more events, so it costs a bit more but gives better CPU detail.
Attach to a running JVM
When the process is already running, use jcmd. No restart is needed.
jcmd <pid> JFR.start name=adhoc settings=profile
jcmd <pid> JFR.dump name=adhoc filename=/tmp/adhoc.jfr
jcmd <pid> JFR.stop name=adhocJFR.start begins a named recording. JFR.dump writes what has been captured so far without stopping it. JFR.stop ends the recording. Find the process ID with jcmd and no arguments, which lists running JVMs.
Give it something to find
Run the slow program from the earlier chapters under JFR. It concatenates strings in a loop, so copying and allocation should dominate.
import java.util.ArrayList;
import java.util.List;
public class Slow {
public static void main(String[] args) {
long start = System.nanoTime();
for (int round = 0; round < 200; round++) {
String s = "";
for (int i = 0; i < 5_000; i++) {
s += i;
}
if (s.length() == 0) {
System.out.println("unreachable");
}
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ms");
}
}Compile it and record it:
javac Slow.java
java -XX:StartFlightRecording=filename=slow.jfr,settings=profile SlowWhen the program exits, JFR writes slow.jfr. Because the program is short, no duration is needed.
Check the file from the command line
The jfr tool in the JDK's bin directory can inspect a file without a GUI:
jfr summary slow.jfr
jfr print --events jdk.ExecutionSample slow.jfrsummary lists event types and counts. jdk.ExecutionSample events are the CPU samples, each with a stack trace. You should see many samples with frames in String and StringBuilder methods.
Common errors
- Recording a quiet period. If the problem is not happening during the recording, the file shows nothing useful. Start the recording before you reproduce the issue.
- Too short a recording. With few samples, results are noise. Aim for tens of seconds under realistic load.
- Wrong settings.
defaultmay not show enough CPU detail for a hot-method search. Switch toprofilefor investigations. - Missing file. If the JVM is killed abruptly, the file may never be written. Use
JFR.dumpto save data while the process is alive.
Exercise
You need a recording from a production JVM that is already running and must not be restarted. Which commands do you run to capture the last stretch of activity into a file, and then stop recording?
Show answer
Run jcmd <pid> JFR.start name=adhoc settings=profile, reproduce or wait for the problem, then jcmd <pid> JFR.dump name=adhoc filename=/tmp/adhoc.jfr to write the data, and finally jcmd <pid> JFR.stop name=adhoc to end the recording.