0Pricing
Java Academy · Lesson

Java Flight Recorder

Low-overhead profiling.

Java Flight Recorder is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is JFR?

Java Flight Recorder (JFR) is a profiling and event-collection engine built directly into the JVM.

It records detailed runtime events — method samples, allocations, GC, locks, IO — to a binary .jfr file, with very low overhead. It ships with the JDK; no extra agent needed.

Why Low Overhead Matters

Traditional instrumenting profilers can slow an app by 2x or more, distorting the very timings you want.

JFR is designed to run in production with typically around 1% overhead. That means you can leave it on and capture a recording when a real incident happens, not just in a lab.

Starting a Recording at Launch

Enable JFR with a JVM flag when you start your application:

  • -XX:StartFlightRecording=duration=60s,filename=app.jfr

This records 60 seconds of events, then writes them to app.jfr. You can also start a recording that runs until the JVM exits.

Controlling with jcmd

You can also start, stop, and dump recordings on a running JVM using jcmd:

  • jcmd <pid> JFR.start name=rec settings=profile
  • jcmd <pid> JFR.dump name=rec filename=out.jfr
  • jcmd <pid> JFR.stop name=rec

This is ideal for grabbing a snapshot from a process that is already misbehaving.

Settings: default vs profile

JFR ships two built-in configurations:

  • default — minimal overhead, safe for continuous production use.
  • profile — more detailed sampling, slightly higher cost, for focused investigation.

Choose with settings=profile. You can also write a custom .jfc config to tune individual events.

What JFR Records

JFR captures a rich set of events, including:

  • Execution samples (where CPU time goes).
  • Object allocation and TLAB activity.
  • Garbage collection pauses.
  • Lock contention (jdk.JavaMonitorWait).
  • File and socket IO, thread parks, and exceptions.

Each event has a timestamp, duration, and stack trace.

Custom Events

You can define your own JFR events to time business logic. Extend jdk.jfr.Event, annotate it, then begin() / commit() around the work.

This is the standalone-friendly part of JFR's API. Real recording still needs the JVM flags, but the event class compiles and runs on any JDK.

import jdk.jfr.Event;
import jdk.jfr.Label;

public class Main {
    @Label("Order Processing")
    static class OrderEvent extends Event {
        @Label("Order Id") int orderId;
    }

    public static void main(String[] args) {
        OrderEvent e = new OrderEvent();
        e.orderId = 42;
        e.begin();
        // ... do the work being measured ...
        e.commit();
        System.out.println("Custom JFR event committed for order " + e.orderId);
    }
}

Continuous Recording

For long-lived services, run a continuous recording with a capped size or age (a ring buffer).

Old events are dropped as new ones arrive, so disk use stays bounded. When an incident occurs you dump the buffer and instantly have the minutes leading up to it.

Dumping on Exit

Add dumponexit=true to a start-flight-recording flag and JFR writes the buffer when the JVM shuts down.

Combine with -XX:FlightRecorderOptions for buffer sizing. This guarantees you capture data even from a process that is about to die.

Reading the File

A .jfr file is binary. You analyze it visually in JDK Mission Control (next lesson), or programmatically with the jdk.jfr.consumer API and RecordingFile.

The command line jfr print app.jfr can also dump events as text for quick grepping.

Streaming Events Live

Since JDK 14, JFR supports event streaming via the jdk.jfr.consumer.RecordingStream API.

Instead of dumping a file and opening it later, you subscribe to events as they happen and react in real time — for example, alerting when GC pauses exceed a threshold. This turns JFR into a live monitoring source, not just a post-mortem tool.

Quick Check

What is the main reason JFR can be left running in production?

Recap

JFR is the JVM's built-in flight data recorder:

  • Low-overhead, production-safe event collection to .jfr files.
  • Start via -XX:StartFlightRecording or jcmd JFR.start.
  • Choose default or profile settings; define custom events.
  • Use continuous ring-buffer recording and dumponexit.
  • Analyze in JDK Mission Control next.

Frequently asked questions

Is the “Java Flight Recorder” lesson free?

Yes — the full text of “Java Flight Recorder” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Java Flight Recorder”?

Low-overhead profiling. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Java Flight Recorder” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Identifying Bottlenecks
  2. Java Flight Recorder
  3. Analyzing with JDK Mission Control
  4. Common JVM Tuning Flags
← Back to Java Academy