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.
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:
Install the Perf tool for your particular kernel release. Use
uname -rto 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
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:
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.
Press Ctrl+Shift+A to open the Find Action dialog, type
Experimental features, and press Enter.
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, or click the button on the toolbar.
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 if you don't use tool buttons).
To stop the profiler prior to stopping the application, use the button in the CPU Profiler tool window.
Export profiler results
From the main menu, selectto open the profiler tool window.
On the left frame of the tool window, click Export Profiler Results.
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.
- 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:
- 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 and buttons.
- 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.