This demo shows how to create a custom JDK Flight Recorder (JFR) event and use it in a native executable.
JFR is a tool, built into the JVM, to collect diagnostic and profiling data in a running Java application.
GraalVM Native Image supports JFR events and users can use the jdk.jfr.Event
API with a similar experience to using JFR in the JVM.
Follow the steps below to record JFR events when running a native executable, enable JFR support, and JFR recording.
Note: JFR event recording is not yet supported on GraalVM JDK for Windows.
- Download and install the latest GraalVM JDK using SDKMAN!.
sdk install java 21.0.1-graal
- Download the demos repository or clone it as follows:
git clone https://github.com/graalvm/graalvm-demos
- Navigate into the native-jfr-demo directory:
cd graalvm-demos/native-jfr-demo
-
Compile the Java file using the GraalVM JDK:
$JAVA_HOME/bin/javac JFRDemo.java
It creates two class files: JFRDemo$HelloWorldEvent.class and JFRDemo.class.
-
Build a native executable with VM inspection enabled:
$JAVA_HOME/bin/native-image --enable-monitoring=jfr JFRDemo
The
--enable-monitoring=jfr
option enables features such as JFR that can be used to inspect the VM. -
Run the executable, emitting standard JFR events, and start recording:
./jfrdemo -XX:+FlightRecorder -XX:StartFlightRecording="filename=recording.jfr"
This command runs the application as a native executable. The
-XX:+FlightRecorder
and-XX:StartFlightRecording
options enable the built-in Flight Recorder and start recording to a specified binary file, recording.jfr. -
Start VisualVM to view the contents of the recording file in a user-friendly way.
-
Go to File, then Add JFR Snapshot, browse recording.jfr, and open the selected file. Confirm the display name and click OK. Once opened, there is a bunch of options you can check: Monitoring, Threads, Exceptions, etc., but you should be mostly interested in the events browsing. It will look something like this:
Alternatively, you can view the contents of the recording file in the console window by running this command:
$JAVA_HOME/bin/jfr print recording.jfr
It prints all the events recorded by Flight Recorder.
-
Learn more about JDK Flight Recorder (JFR) with Native Image and how to further configure JFR recording and system logging.