IntelliJ IDEA 2019.1 Help

CPU Profiler

CPU Profiler monitors JVM-level parameters of your application to provide a better understanding of how your application is executed, and how exactly resources are allocated. This data can help you find and resolve performance problems.

CPU Profiler shows what functions consume what percent of CPU time. This information can provide you a better understanding of how your application is executed, and how exactly resources are allocated.

You can run the profiler for projects built with Gradle and with the native IntelliJ IDEA build tool.

Once the analysis is finished, the profiler visualizes the output data in the reports to make them more understandable.

CPU Profiler

Before you start using CPU Profiler, enable this option in the list of experimental features. On Linux, you also need to install the Perf tool and adjust kernel options.

Install Perf and adjust kernel options on Linux

If you're using Linux, make sure to complete the following steps:

  1. Install the Perf tool for your particular kernel release. Use uname -r to find out the exact version, and then install the corresponding linux-tools package. For example:

    marinak@marinak-VirtualBox:~$ uname -r 4.15.0-36-generic marinak@marinak-VirtualBox:~$ sudo apt-get install linux-tools-4.15.0-36-generic

  2. Adjust kernel options. Before starting the profiler on Linux, you need to set up two kernel options:

    • perf_event_paranoid - controls the use of performance events data by non-root users. Set its value to be less than 2 to let the profiler collect performance information without root privileges:

      sudo sh -c 'echo 1 >/proc/sys/kernel/perf_event_paranoid'

    • kptr_restrict sets restrictions on exposing kernel addresses. To have kernel symbols properly resolved, disable the protection offered by kptr_restrict by setting its value to 0:

      sudo sh -c 'echo 0 >/proc/sys/kernel/kptr_restrict'

    By default, these changes affect your current OS session only. To keep the settings across system reboots, run:

    sudo sh -c 'echo kernel.perf_event_paranoid=1 >> /etc/sysctl.d/99-perf.conf' sudo sh -c 'echo kernel.kptr_restrict=0 >> /etc/sysctl.d/99-perf.conf' sudo sh -c 'sysctl --system'

    Upon the first launch of the profiler, IntelliJ IDEA checks whether kernel variables are already set up and suggests the necessary changes:

    adjust linux kernel variables for the profiler

Enable CPU Profiler in experimental features on Linux and macOS

CPU Profiler is still an experimental feature and it's not enabled by default. To use the profiler, you need to enable it.

  1. Press Ctrl+Shift+A to open the Find Action dialog, type Experimental features, and press Enter.

  2. Select the checkbox next to java.profiler.enabled, apply the changes, and close the dialog.

Using the profiler

Run the profiler

  • From the main menu, select Run | Run 'class name' with Async Profiler, or click the Profiler button on the toolbar.

Profiler popup informing that the data is ready

After the application stops, and the profiling data is ready, you will see a confirmation popup, and the CPU Profiler tool button will appear. Click this button to open the CPU Profiler tool window (or go to View | Tool Windows | CPU Profiler if you don't use tool buttons).

To stop the profiler prior to stopping the application, use the Stop button in the CPU Profiler tool window.

Export profiler results

  1. From the main menu, select View | Tool Windows | CPU Profiler to open the profiler tool window.

  2. On the left frame of the tool window, click Export Export Profiler Results.

  3. In the dialog that opens, name the file and specify a folder in which you want to save it. Click Save.

Interpreting the results

In the CPU Profiler tool window, the collected data is presented on three tabs - Flame Chart, Call Tree, and Method List. The left-hand part lists application threads; by clicking on each thread you can go in more details.

CPU Profiler
Flame Chart

This tab shows you the state of the call stack at any moment of time in the performance profile. Each block represents a function in the stack (a stack frame). On the Y-axis, there is a stack depth going from bottom up. The X-axis shows the stack profile sorted from the most CPU-consuming functions to the least consuming ones.

When you read the flame chart, focus on the widest blocks. These blocks are the functions that are presented in the profile most. You can start from the bottom and move up, following the code flow from parent to child methods, or use the opposite direction to explore the top blocks that show the functions running directly on the CPU.

In the Flame Chart tab, you can hover the mouse over any block to view the details:

block details in the flame chart

Call Tree

The Call Tree tab shows the program call tree with the percentage of each function in the total profiling time. It organizes the data to show you where the application spends most of the time in total. To configure and filter the Call Tree view, use the Settings and Filter buttons.

call tree tab in the profiler results

Method List

The Method List tab shows the list of methods sorted by the number of samples. For each method from the list, the profiler provides more information on the nested tabs. The Back Traces tab shows where the selected method has been called. The Merged Callees tab shows call traces that started from the selected method.

method list tab in the profiler results
Last modified: 13 May 2019