Sitemap
Nuwan Zen

Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!

Java 11: Flight Recorder

2 min readApr 4, 2021

Java 11 made Flight Recorder free to use.

Press enter or click to view image in full size
Photo by Ross Parmly on Unsplash

Download JMC(Java Mission Control) from here https://jdk.java.net/jmc/ to Analyse the Flight Recorder output.

Create this file which will generate java.lang.OutOfMemoryError: Java heap space error.

package com.nuwan.java11;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class JavaFlightRecorder {
private static BlockingQueue<byte[]> queue=new LinkedBlockingQueue<>();

public static void main(String[] args) {
Runnable producer = () -> {
while (true) {
queue.offer( new byte[1*1024*1024]);
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

Runnable consumer = () -> {
while (true){
try {
queue.take();
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

new Thread(producer,"Producer Thread").start();
new Thread(consumer,"Consumer Thread").start();
}
}

After compiling the file run as below.

java -XX:StartFlightRecording=duration=30s,settings=profile,filename=leak.jfr JavaFlightRecorder

As per this example java file recorder file will be created in the same place where java class is located. To analyze the file you can use JMC which we already downloaded. Use below reference link for to find how to analyze via JMC.

Get Nuwan Zen’s stories in your inbox

Join Medium for free to get updates from this writer.

Ref: https://mkyong.com/java/java-11-java-flight-recorder/

JCMD for Running Java process

We can use jcmd to monitor running java processors.

Let’s see what are the running processors at the moment:

jcmd

This command will list down all the java processors running. Now let’s start JFR for the application we need.

jcmd 19073 JFR.start duration=30s settings=profile filename=nuwan-jfr.jfr

File we be generated and you can analyse it by openning it through JMC.

My Other Blogs:

--

--

Nuwan Zen
Nuwan Zen

Written by Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!