Java Flight Recorder

JVM · lesson 16 of 34 · 3 min read

A profiler built into the JVM itself, cheap enough to leave switched on in production.

Open this lesson in the learning hub

Key points

  • JFR records events the VM already knows about: allocations, GC pauses, lock contention, exceptions, IO and cpu samples.
  • Overhead on the default profile is around one percent, because the VM emits the events rather than a tool polling from outside.
  • Start it at launch with -XX:StartFlightRecording, or attach to a running process later with jcmd.
  • Open the .jfr file in JDK Mission Control. Its automated analysis ranks the likely problems before you look at anything.
  • The real win: a 60 second recording from the box that is actually misbehaving beats hours of guessing locally.

Example

# record 60 seconds from a process that is already running
jcmd <pid> JFR.start name=hot settings=profile duration=60s filename=/tmp/hot.jfr
jcmd <pid> JFR.check
jcmd <pid> JFR.dump name=hot filename=/tmp/hot.jfr

# or record continuously from startup, capped in size
java -XX:StartFlightRecording=settings=profile,filename=/var/log/app.jfr,maxsize=200M -jar app.jar

# then open the file in JDK Mission Control
jmc

JFR is the profiler you can afford to leave running.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the JVM course, and every lesson in it is listed on the JVM contents page.