Using Java Flight Recorder (JFR) in Quarkus
Java Flight Recorder (JFR) is a powerful tool for collecting diagnostic and profiling data about a running Java application. It provides detailed insights into the application's behavior, such as method calls, memory allocations, and thread activity. Quarkus is a Kubernetes-native Java framework that is designed to be fast and lightweight. In this blog post, we will explore how to use JFR in Quarkus applications.
Table of Contents#
- What is Java Flight Recorder (JFR)?
- Enabling JFR in Quarkus
- Configuring JFR in Quarkus
- Using JFR in Quarkus Applications
- Analyzing JFR Data
- Best Practices for Using JFR in Quarkus
- Conclusion
- References
What is Java Flight Recorder (JFR)?#
Java Flight Recorder (JFR) is a low-overhead profiling and diagnostic tool that is built into the Java Virtual Machine (JVM). It allows developers to collect detailed information about the behavior of a running Java application, such as method calls, memory allocations, and thread activity. JFR data can be used to diagnose performance issues, identify memory leaks, and understand the overall behavior of the application.
Enabling JFR in Quarkus#
JFR is built into the JVM and does not require a Quarkus-specific extension. To enable JFR in a Quarkus application, you can use standard JVM arguments when starting your application. For example, you can enable JFR by adding the following arguments to your startup command:
java -XX:StartFlightRecording=name=quarkus-app,duration=60s -jar my-app.jarYou can also enable JFR to start automatically when the application starts by using the -XX:StartFlightRecording option with appropriate settings.
Configuring JFR in Quarkus#
You can configure JFR using standard JVM arguments or programmatically via the jdk.jfr.Recording API. Here are some common configuration options:
JVM Arguments#
-XX:StartFlightRecording: Enables and starts a JFR recording-XX:FlightRecorderOptions: Specifies JFR options such asdefaultrecording=true,settings=profile
Example Configuration via application.properties#
While there are no Quarkus-specific JFR properties, you can pass JVM arguments through Quarkus configuration:
quarkus.jvm.args=-XX:StartFlightRecording=name=quarkus-app,settings=profile,dump-on-exit=trueProgrammatic Configuration#
You can also configure JFR programmatically using the jdk.jfr.Recording API as shown in the next section.
Using JFR in Quarkus Applications#
Once you have enabled and configured JFR in your Quarkus application, you can start using it to collect data. Here are some ways to use JFR in Quarkus applications:
Programmatically#
You can start and stop JFR recordings programmatically in your Quarkus application. Here is an example of how to start a JFR recording:
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingFile;
import javax.enterprise.context.ApplicationScoped;
import java.io.IOException;
@ApplicationScoped
public class JfrService {
private Recording recording;
public void startRecording() throws IOException {
recording = new Recording();
recording.setName("my-app");
recording.start();
}
public void stopRecording() throws IOException {
recording.stop();
RecordingFile.writeAll(recording, "/tmp/my-app.jfr");
}
}Using the JFR Command-Line Tool#
You can also use the JFR command-line tool to start and stop JFR recordings. Here is an example of how to start a JFR recording:
java -XX:StartFlightRecording=name=my-app,settings=profile -jar my-app.jarHere is an example of how to stop a JFR recording:
jcmd <pid> JFR.stop name=my-appAnalyzing JFR Data#
Once you have collected JFR data, you can analyze it using the Java Mission Control (JMC) tool. JMC is a graphical tool that allows you to view and analyze JFR data. Here are the steps to analyze JFR data using JMC:
- Download and install JMC from the Oracle website.
- Open JMC and select
File > Opento open the JFR file. - Use the JMC UI to view and analyze the JFR data.
Best Practices for Using JFR in Quarkus#
Here are some best practices for using JFR in Quarkus applications:
- Enable JFR in Production: JFR has a very low overhead, so it is safe to enable it in production applications.
- Use JFR Settings Wisely: Choose the appropriate JFR settings for your application. The
defaultsettings are suitable for most applications, but you may need to use other settings for specific profiling or diagnostic purposes. - Dump JFR Recordings on Shutdown: Dumping JFR recordings on shutdown can be useful for debugging issues that occur during application startup or shutdown.
- Analyze JFR Data Regularly: Analyze JFR data regularly to identify performance issues, memory leaks, and other problems.
Conclusion#
In this blog post, we have explored how to use Java Flight Recorder (JFR) in Quarkus applications. We have learned how to enable and configure JFR, how to use JFR programmatically and using the command-line tool, and how to analyze JFR data using Java Mission Control (JMC). We have also discussed some best practices for using JFR in Quarkus applications. By following these best practices, you can use JFR to collect valuable data about your Quarkus applications and improve their performance and reliability.