Profiling Java 8 with Java Flight Recorder and Java Mission Control in Eclipse

Jan LouwerensBackend Team Lead
CERTIFIED EXPERT
Published:
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collection framework built into the Oracle JDK. It allows Java administrators and developers to gather detailed low level information about how the Java Virtual Machine (JVM) and the Java application are behaving. Java Mission Control is an advanced set of tools that enables efficient and detailed analysis of the extensive of data collected by Java Flight Recorder. The tool chain enables developers and administrators to collect and analyze data from Java applications running locally or deployed in production environments.

The Java Flight Recorder does not trace every function call that gets executed. Instead, it "samples" the application at regular intervals, collecting stack trace information about each thread being executed. So if you're looking for how many times a certain function is being called, or how much total time is spent executing a certain method, you're not going to find that kind of information. However, by analyzing the samples taken, you can still determine where your application is spending most of its time.

We'll assume, at this point, that Java 8 and Eclipse are already installed. If they are not, they can be found at the following locations:
Additionally, a couple of eclipse plugins, that contain the Java Flight Recorder and Java Mission Control tools, will need to be installed. This can be done within Eclipse by going to:
To make things simpler, we'll also install the Flight Recorder Launch Configuration Tab tool:

Once that's complete, create a simple java application that could use some optimization:
   public static void main(String[] args)
                         {
                            // These values may need to change depending upon the speed of your machine.
                            // You want this app to run for approximately 60 seconds to get usable results
                            final int outerIterations = 1000;
                            final int innerIterations = 10000;
                      
                            for (int outer = 0; outer < outerIterations; outer++)
                            {
                               StringBuilder message = new StringBuilder();
                      
                               for (int inner = 0; inner < innerIterations; inner++)
                               {
                                  message.append(inner);
                               }
                      
                               System.out.println(message);
                            }
                         }

Open in new window


Create a Run Configuration for the java application:
  • Run -> Run Configurations...
    • Java Application -> New
Within the Run configuration, select the Flight Recorder tab.
Make sure the following two options are checked:
  • Enable - This will create a flight recording of the running application
  • Open recording - This will automatically open the flight recording when the application completes

Now run the application using the Run configuration. Once the application is finished, and the flight recording is open, we can see a lot of information about the running application.
  • Select the Code tab on the left.
    • From here we can see the packages and classes in which the application is spending its time.
  • Select the "Hot Methods" tab on the bottom (still within the Code tab on the left)
    • Here we can see the methods in which the application is spending its time
  • Select the "Call Tree" tab on the bottom
    • Here we can see the stack traces in which the application is spending most of its time
With the information collected in these places, we can see that this sample application is spending most of its time converting the ints to chars when appending them to the StringBuilder.
  • Select the Memory tab on the left
  • Select the Garbage Collections tab on the bottom (still within the Memory tab on the left)
Here we can see in the graph the memory that gets used as we append to the StringBuilder. We can also see when the garbage collector releases the memory, after each iteration of the inner loop, when the StringBuilder no longer has a reference.
2
5,984 Views
Jan LouwerensBackend Team Lead
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.