How to collect Java Flight Recorder (JFR) diagnostic data for Java Application

Requirement:

Like other many diagnostic mechanism provided by web servers or applications, Java Flight Recorder is a powerful tool provided by Java to diagnose applications running on Java. This may not be helpful to provide you application specific issue however this is extremely helpful to debug issued observed around Java memory utilization, Java run time environment or different java threads.

There are plenty of document online mentioned advantage of JFR in debugging applications however we are more interested to understand how to enable it in our system to capture data for analysis with sometime to provide to support engineer.

This topic cover basic of JFR and does not have advance feature/options example or explanations.

Steps to Enable JFR :

By default JFR is not enabled. Its commercial feature hence only available on commercial version. There are several way you can enable it. We will discuss two different way over here. To keep it simple we can say Automatic and Manual data collection with default option.

Auto:

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=dump.jfr YourJavaApplication

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -> You have to start your application by passing these two parameter.

As soon as you application start this will start collecting data of JVM and YourJavaApplication for next 60sec, and will write that to dump.jfr file after 60sec.

Additionally you can use -XX:FlightRecorderOptions to pass some more options like debug level  :  -XX:FlightRecorderOptions=loglevel=debug

This is a contineous recording and once you pass the parameters and start your application recording is done by JFR as per input. However I like personally like below manual approach where it gives me opportunity to change/modify my recording parameters.

Tips for Weblogic: To enable JFR on weblogic managed servers you can put –XX:+UnlockCommercialFeatures -XX:+FlightRecorder parameter in server start property on weblogic console or add that in config.xml(domain_home/domain_name/config/config.xml) file. You have to restart your managed server to changes take effect.

<server>
<name>managedServer</name>

<sever-start>
<arguments>-XX:+UnlockCommercialFeatures -XX:+FlightRecorder</arguments>
</server-start>

</server>

Manual:

For this option you just pass below 2 parameters while starting your application.

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder YourApplication

Once your application started note the PID of it. (quick e.g: ps -ef | grep YourApplicationName )

JFR provide below 4 options to start/stop recordings.

JFR.start: Start Recording

jcmd <pid_of_your_application> JFR.start duration=60s filename=myrecording.jfr

Duration can be provided in seconds(s), minutes(m), hours(h) or days(d). File will be written in to your running application home or current directory.

  JFR.check: All Recording Status of pid provided

jcmd <pid_of_your_application> JFR.check

This give you status of all currently running or stopped recording with unique recording number.

Recording: recording=1 name="myrecording.jfr" duration=30m filename="myrecording.jfr" compress=false (stopped)
Recording: recording=2 name="myrecording2.jfr" duration=3h filename="myrecording2.jfr" compress=false (running)

  JFR.stop: Stop recording and write the collected data to file

This will the recording and write all collected data in provided filename.

jcmd <pid_of_your_application> JFR.stop recording=2

  JFR.dump : dump collected data to file and continue recording

jcmd <pid_of_your_application> JFR.dump recording=2 filename=myrecording2dump.jfr

This will allow you to analyse collected data so far while JFR is continuing with recording.

I Love Cooking Too, Please visit my Cooking Channel on YouTube: https://www.youtube.com/c/priyashreefoodfashion

Please do Subscribe on YouTube if you are a foodie too 🙂

Recipe: Khandvi

Leave a comment