Flight Recorder

Quick Summary:

  • Java Flight Recorder (JFR).
  • A low overhead data collection framework for performance monitoring and troubleshooting Java applications and the HotSpot JVM.
  • Involves using the provided API in jdk.jfr, which produces a .jfr-file, which we can analyse using a tool like JDK Mission Control.
  • Released in Java 11 (2018).

Key Features

  • Java Flight Recorder is akin to a “black box” in an aircraft that records important events and metrics during the runtime of Java applications.
  • Originally a commercial feature in Oracle JDK, JFR has been open-sourced in Java 11, making it accessible without additional cost.
  • It captures a wide array of detailed data about the JVM operations, such as garbage collection, heap usage, and threading information.
  • This data can be used for post-event analysis and debugging, offering a deep insight into the Java application performance characteristics.

Example

Here’s an example of how to use Java Flight Recorder:

import jdk.jfr.*;

public class FlightRecorderDemo {
    public static void main(String[] args) {
        try{
            Configuration config = Configuration.getConfiguration("default");
            try (Recording recording = new Recording(config)) {
                recording.start();

                // Your application logic here

                recording.stop();

                // Save recording to a file
                recording.dump(Path.of("jfr", "recording.jfr"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • This example sets up JFR with the default configuration, starts the recording, and stops after the application logic completes.
  • The recording can be saved to a file for later analysis using JDK Mission Control or another analysis tool.

Analysing the .jfr Recording File

Once Java Flight Recorder (JFR) has collected data during a Java application’s runtime, it outputs this information into a .jfr file. This file can then be analysed to gain detailed insights into the application’s performance and behaviour.

Tools for Analysis

  • JDK Mission Control (JMC): The primary tool recommended for analyzing .jfr files. It provides a comprehensive GUI that displays various metrics collected during the recording. JDK Mission Control allows users to drill down into detailed metrics about the JVM and application performance.
  • Command-line tools: For automated analysis or environments where GUI is not feasible, JDK provides command-line tools to parse and analyse .jfr files.

Steps to Analyse a .jfr File with JMC

  1. Open JMC: Start JDK Mission Control (download & install if necessary).
  2. Load the .jfr File: Go to the “File” menu, select “Open File,” and navigate to the location of your .jfr file. Select the file to open it in JDK Mission Control.
  3. Explore the Dashboard: Once the file is loaded, you’ll be presented with the JDK Mission Control dashboard. This dashboard provides a high-level overview of various metrics.
    • Overview Tab: Shows a summary of the recording, including JVM information and a high-level view of CPU and memory usage over time.
    • Memory Tab: Detailed information on memory usage, including heap and non-heap memory, garbage collection statistics, and object statistics.
    • Threads Tab: Information about thread activity, thread dumps, and lock instances.
    • Events Tab: Detailed list of recorded events, which can be filtered by event type (e.g., GC events, exceptions, I/O operations).
  4. Analyze Specific Metrics: Utilize the various tabs and tools within JDK Mission Closure to delve into specific areas of interest. For instance:
    • Analyze “Hot Methods” to identify methods that consume significant CPU time.
    • Look at the “Gararage Collector” statistics to understand the behavior and impact of GC on your application’s performance.
    • Examine thread activities to identify concurrency issues or deadlocks.
  5. Generate Reports: Generate detailed reports or export data for further analysis or documentation purposes.

👩‍💻 Hands-on Demo: Flight Recorder

  1. In a new FlightRecorderDemo-class, write a program that starts a JFR Recording with default configuration and dumps the result in a file (e.g. “/jfr/recording.jfr“).
  2. Add reasonably intensive bit of logic between the .start() and .stop(): print the first 50 prime numbers.
  3. Run your application and double-check that a .jfr file was produced.
  4. (Extra) Open the .jfr file using JMC.
  5. (Extra) Check if your IDE supports profiling with JFR and use that integration instead of JMC.

Solutions

🕵️‍♂️ Click here to reveal the solutions
public class FlightRecorderDemo {
    public static void main(String[] args) {
        try{
            Configuration config = Configuration.getConfiguration("default");
            try (Recording recording = new Recording(config)) {
                recording.start();

                // Your application logic here
                printFirstNPrimeNums(50);

                recording.stop();

                // Save recording to a file
                recording.dump(Path.of("jfr", "recording.jfr"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void printFirstNPrimeNums(int target) {
        int i = 2;
        int primeNumsFound = 0;
        while (primeNumsFound < target) {
            if (isPrime(i)) {
                System.out.println(i);
                primeNumsFound++;
            }
            i++;
        }
    }
    private static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Extra:


Summary

  • Java Flight Recorder (JFR): Introduced in Java 11 (2018) under JEP 328, JFR captures vital runtime data with minimal overhead, functioning like a “black box” for Java applications.
  • Accessibility and Functionality: Open-sourced in Java 11, JFR allows developers to monitor performance efficiently using the jdk.jfr API, with capabilities to start, stop, and save recordings easily.
  • Data Analysis: JFR files can be analyzed using JDK Mission Control, providing insights into CPU usage, memory statistics, and thread activity, helping identify and resolve performance bottlenecks.