๐ Mastering Java Flight Recorder (JFR): A Complete Guide to Java Performance Profiling
Java Flight Recorder (JFR) is one of the most powerful and underutilized tools in the Java ecosystem. Developed by Oracle, it provides deep insights into JVM behavior with minimal performance overhead, making it ideal for diagnosing production issues and performance bottlenecks.
Whether you're a seasoned backend engineer, a Java performance tuner, or simply looking to understand what's happening under the hood, this guide will walk you through everything you need to know about JFR — from what it is, how to use it, and real-world use cases.
๐ What is Java Flight Recorder?
Java Flight Recorder (JFR) is a profiling and event collection framework built directly into the JVM. It records diagnostic and profiling data about a running Java application, including CPU usage, memory allocation, thread activity, garbage collection events, and more.
✅ Low overhead – Safe to use in production
✅ Built-in with the JDK – Available since JDK 7u40 (commercial), and open-sourced from JDK 11
✅ Highly detailed data – Native integration provides insights impossible to get from external profilers
๐ ️ Why Use Java Flight Recorder?
-
๐ Diagnose performance issues
-
๐ง Understand application behavior
-
๐งต Analyze thread contention and deadlocks
-
♻️ Tune garbage collection
-
๐ Track memory leaks and CPU spikes
๐ฆ Enabling JFR in Your Java Application
From JDK 11 onwards, JFR is included in the OpenJDK distribution and enabled by default.
1. Start with the JFR Command Line
You can enable JFR directly from the command line when launching your Java application:
java -XX:StartFlightRecording=filename=recording.jfr,duration=60s,settings=profile -jar your-app.jar
-
filename=recording.jfr
– Output file for the recording -
duration=60s
– Automatically stops recording after 60 seconds -
settings=profile
– Use the "profile" configuration (more detailed than "default")
2. Start and Stop JFR at Runtime (JDK 14+)
jcmd <PID> JFR.start name=myrecording settings=profile duration=120s filename=myrecording.jfr
List running recordings:
jcmd <PID> JFR.check
Stop a recording manually:
jcmd <PID> JFR.stop name=myrecording
๐จ Visualizing the Recording
Using JDK Mission Control (JMC)
JDK Mission Control is a powerful GUI tool to analyze .jfr
files.
Features:
-
Timeline view of CPU, memory, threads
-
Thread dumps and stack traces
-
Garbage Collection visualizations
-
Event browser with filtering
๐ก Tip: Install JMC and associate it with .jfr
files for quick double-click analysis.
⚙️ Common Events Captured by JFR
Event Type | Description |
---|---|
CPU Load | JVM and system CPU usage |
Memory Usage | Heap, metaspace, GC activities |
Thread States | Thread start, block, wait, runnable states |
Class Loading | When classes are loaded/unloaded |
Exceptions | Types and frequency of exceptions thrown |
Locks & Contention | Monitor enter/exit, park/unpark, deadlocks |
๐งช Real-World Use Case: Troubleshooting a CPU Spike
Imagine you're seeing a sudden CPU spike in your production server. Instead of blindly adding logs or restarting the app, use JFR:
-
Trigger a flight recording with a 5-minute window.
-
Analyze the
.jfr
file in JDK Mission Control. -
Identify hot threads and methods consuming the most CPU.
-
Correlate with request logs to narrow down the source.
-
Fix the code or tune the configuration.
✅ Faster RCA (Root Cause Analysis)
✅ Less downtime
✅ More confidence in changes
๐งฐ Best Practices for Using JFR in Production
-
Use default settings for low overhead unless deeper profiling is needed.
-
Record during off-peak hours for longer duration analysis.
-
Store recordings securely — they may contain sensitive information.
-
Combine with alerts and monitoring tools (e.g., Prometheus, Grafana) for proactive profiling.
๐ JFR vs Other Profiling Tools
Feature | JFR | VisualVM / YourKit / JProfiler |
---|---|---|
Overhead | Very Low | Medium to High |
Production Safe | ✅ Yes | ⚠️ Not recommended always |
Built-in Support | ✅ From JDK 11 | ❌ Requires installation |
Recording Granularity | High | High |
๐ก️ Security and Compliance Considerations
-
Avoid sharing
.jfr
files publicly; they might contain:-
Stack traces
-
Exception messages
-
Class names and internal logic
-
-
If sharing is required (e.g., with Oracle), sanitize the recording first.
๐งญ Summary
Java Flight Recorder is an indispensable tool for modern Java developers and architects. It provides deep insight with minimal overhead, making it ideal for real-time performance monitoring and diagnosis. Unlike traditional profilers, it is production-safe and built into the JDK.
Start using JFR today to gain a better understanding of your application's runtime behavior and to solve performance issues like a pro.
Sign up here with your email
ConversionConversion EmoticonEmoticon