60

Using Java Flight Recorder to Profile Spring Boot applications

 5 years ago
source link: https://www.tuicool.com/articles/hit/fy2AzeN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Few months back I had to do performance optimisation of a low latency application. The tool that helped me a lot was Java Flight Recorder. Today, I had to do some similar work and I completely forgot how I was able to launch flight recorder GUI. In this short post, I will show you the process that I followed to create flight recorder recordings.

From the official documentation ,

 Java Flight Recorder (JFR) is a tool for collecting diagnostic and profiling data about a running Java application. It is integrated into the Java Virtual Machine (JVM) and causes almost no performance overhead, so it can be used even in heavily loaded production environments. When default settings are used, both internal testing and customer feedback indicate that performance impact is less than one percent. For some applications, it can be significantly lower. However, for short-running applications (which are not the kind of applications running in production environments), relative startup and warmup times can be larger, which might impact the performance by more than one percent. JFR collects data about the JVM as well as the Java application running on it. 

Java Flight Recorder is a commercial tool but you can use it for development. Starting with the release of Oracle JDK 7 Update 40 (7u40), Java Flight Recorder is included in the JDK so no additional download is necessary.

Step 1: Start the Java Mission Control

Java Mission Control is the GUI tool that integrates Java Flight Recorder. If you are using Oracle JDK then it will already be present in your path.

You can launch it using the following command.

jmc

It will open up a desktop application like as shown below.

yuUJzqF.png!web

As you can see on the left you can see all the Java processes running on your machine.

Step 2: Create a Spring Boot application

We will using start.spring.io to create a Spring Boot application.

curl https://start.spring.io/starter.zip \
-d dependencies=web -d groupId=com.shekhargulati -d artifactId=myapp -d name=myapp \
-d baseDir=myapp -o myapp.zip

Unzip and change directory to myapp

unzip myapp.zip && rm -f myapp.zip
cd myapp

Build the application using Maven

./mvnw clean install

This will build the application and create the executable JAR.

You can run the app using java -jar target/myapp-0.0.1-SNAPSHOT.jar

When the app is running go to the JMC GUI and you will see your process as shown below.

2eiqaqy.png!web

If you click on Flight Recorder then you will get following exception

qMn2Qzv.png!web

 The Flight Recorder is not enabled. If you attempt to start a new Flight Recording, you will get the option to enable the Flight Recorder on the JVM. 

The reason as you have guessed from the error message is because you have not enabled flight recording for your Java process.

Step 3: Enable Java Flight Recorder for your Java process

To enable Java Flight Recorder, you have to start Spring Boot application using the command shown below.

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -jar target/myapp-0.0.1-SNAPSHOT.jar

-XX:+UnlockCommercialFeatures is used to tell Java to unlock commercial features as JFR is a commercial feature.

-XX:+FlightRecorder is used to tell JVM that we want to use JFR.

Now, if you go to Java Mission Control and click on Flight Recorder you will see no error.

YVF3Iji.png!web

You can take a new recording and you will see the result as shown below.

7nmQBzm.png!web

I will recommend you to go over JFR documentation to learn more about its features. I found it a great tool to unlock performance and memory related issues.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK