Introduction to the Java Flight Recorder (JFR)
As a total newbie to the Java flight recorder, I found these posts helpful in understanding the history and goals of JFR:
- Get started with JDK Flight Recorder in OpenJDK 8u | Red Hat Developer – includes links to external resources e.g. the associated JEP 328: Flight Recorder (openjdk.org).
- Monitoring Java Applications with Flight Recorder | Baeldung – decent explanation, including what is and isn’t essential (i.e. which flags are outdated)
The .java_pid file extension came up and I was not familiar with it. The answer at java – How to get rid of /tmp/.java_pid<number> files in Linux? – Stack Overflow explains that these files are created by the JVM to support debugging as part of the attach
api. Here is a more recent link to VirtualMachineImpl.java (and the related VirtualMachineImpl.c). Here is a simple walk-through (from the above posts) showing how to use JFR. I used Ubuntu 22.04 for this. First, download a JDK build.
mkdir -p ~/java/binaries/jdk/x64
cd ~/java/binaries/jdk/x64
sudo apt install curl
curl -Lo microsoft-jdk-11.0.18-linux-x64.tar.gz https://aka.ms/download-jdk/microsoft-jdk-11.0.18-linux-x64.tar.gz
tar -xzf microsoft-jdk-11.0.18-linux-x64.tar.gz
Next, get and compile the Java program to use to experiment with JFR (I used the Red Hat leaks demo).
curl -Lo RedHatLeaksDemo.java https://raw.githubusercontent.com/swesonga/scratchpad/baa0263f480b6d5c5446be90f572b2a7897279fa/demos/java/RedHatLeaksDemo.java
jdk-11.0.18+10/bin/javac RedHatLeaksDemo.java
Starting JFR Using Java Command Line Flags
Use the -XX:Start
flag as suggested by the referenced posts above.
jdk-11.0.18+10/bin/java -XX:StartFlightRecording=duration=5s,filename=flightRedHatLeaks.jfr RedHatLeaksDemo
A message will be displayed notifying you that recording has started.
Started recording 1. The result will be written to:
/home/saint/java/binaries/jdk/x64/flightRedHatLeaks.jfr
Starting JFR Using jcmd
To start a recording after application startup, use jcmd.
# Start the application normally
jdk-11.0.18+10/bin/java RedHatLeaksDemo
Launch a new terminal then run jcmd to start a JFR recording.
# Determine the pid of the java process
ps -a | grep java
jdk-11.0.18+10/bin/jcmd 13573 JFR.start duration=100s filename=flight-jcmd.jfr
Viewing Results Using VisualVM
Download VisualVM or get and launch it using these commands:
mkdir -p ~/java/binaries/visualvm
cd ~/java/binaries/visualvm
curl -Lo visualvm_215.zip https://github.com/oracle/visualvm/releases/download/2.1.5/visualvm_215.zip
unzip visualvm_215.zip
visualvm_215/bin/visualvm --jdkhome ~/java/binaries/jdk/x64/jdk-11.0.18+10
Once VisualVM loads, browse to and open the .jfr file.
Leave a Reply