- Profiling Tools and IntelliJ IDEA Ultimate
- Profiler integrations in IntelliJ IDEA Ultimate
- Configuration Settings
- Run your application with a profiler
- Working with Flame Graphs
- Call Tree
- Method List
- Events
- Attaching to an existing configuration
- Using Search method in the call tree
- Search with filtering in call tree and viewing callees and backtraces
- Import profiling results created by other utilities
- Opening Merge/back
- Custom profiler configurations
- Create a separate profiler configuration
- Async Profiler
- Adjust kernel options on Linux
- Configuration options
- Java Flight Recorder
- Enable JFR for Oracle JDK 8-11
- JFR configurations
- Create a custom JFR configuration (optional)
- Load the custom configuration to the IDE (optional)
- How can I run Java Flight Recorder in the Intellij Idea community development environment with OpenJDK 16
- Answer
Profiling Tools and IntelliJ IDEA Ultimate
IntelliJ IDEA Ultimate has out of the box support for profiling your applications using multiple Profilers. Now you can profile your application and analyze the results, without leaving your IDE. By profiling your application, you can discover the methods that execute in your application and for how long. Using these metrics, you can determine ways to improve the performance of your system.
In this blog post, I’ll walk you through the support that IntelliJ IDEA Ultimate has for profiling your applications. Let’s get started.
Profiler integrations in IntelliJ IDEA Ultimate
IntelliJ IDEA Ultimate integrates multiple profilers. It integrates Java Flight Recorder (JFR) on Windows, macOS, and Linux. On macOS and Linux, the IDE also has integration with Async Profiler.
IntelliJ IDEA Ultimate has been supporting Profiler integration since its version 2018.3, with the Async Profiler.
Configuration Settings
IntelliJ IDEA Ultimate includes out of the box support for the Async and JFR profilers. These profilers can also be configured using settings (Preferences → Build, Execution, Deployment → Java Profiler):
JFR is built into the JVM. To use JFR, you’ll need to Configure your IntelliJ IDEA Ultimate to use a JDK distribution which includes JFR. Starting with Java 11, JFR is included on all JDK distributions. JFR works on Oracle JDK builds starting from version 8 (with its commercial features enabled). Work is in progress on merging JFR into OpenJDK8 tree. Moving forward, vendors are working on including JFR in the various versions of their JDK binaries. So, please check for the inclusion of JFR in the JDK binary you are using.
Support for JFR usage was introduced in IntelliJ IDEA Ultimate 2019.2. If you want to profile your applications using an older IntelliJ IDEA Ultimate version, you can use the Async profiler on Linux and macOS.
Run your application with a profiler
You can start profiling your application in multiple ways. Click on the start icon in the gutter and select ‘Run with ’. You can also choose this option from the ‘Run’ menu, or use Search Everywhere or Find Action to run ‘Run with Profiler’.
The output window displays a message stating ‘Profiler attached’, with a link to ‘Open’, to view the profiled data. Just in case, you miss the popup to view profiled data, you can open the Profiler window by using switcher or by using Find Action.
When you click on ‘Open’, you get an option to stop profiling and view the results:
Here’s capturing all these steps:
With the integrated profilers, you can view the Flame Graph, Call Tree, Method List or Events in IntelliJ IDEA Ultimate.
Working with Flame Graphs
Here’s a screenshot of a flame graph generated by profiling an application using Async CPU Profiler:
Every rectangle in a flame graph represents a function name. Blue rectangles represent native calls and yellow rectangles represent Java calls.
A flame graph is not a time series – it doesn’t show the sequence of calling of methods in an application. It shows which methods are calling other methods (this is how the method stacks are represented), for how long, and where they are executed. This graph might suggest where the problems are and where to optimize. A flame graph is another way of stating: hey, this is what happened when we profiled your application, and we grouped it. Note, the blocks are relative; it represents a snapshot total time on CPU.
A flame graph won’t disclose which tasks were performed or called before or after. It lets you find out the total CPU time that is being spent by your application. For example, if it spends 29% of the time on the methodA() method, that doesn’t mean it takes a lot of time to execute. It could also mean that you are calling it many times. So you could either think of reducing the number of calls to this method or optimize it so it executes in less time.
Call Tree
Flame graphs could be represented in a textual form, using Call Trees, showing how methods are called and the percentage of total CPU time used by them.
Call Tree shows the method execution path in your application. You could use them to get a quick overview of application activity, examine the execution path of slowest methods, determine critical execution paths and much more. Please refer to this link on our documentation page for more details on Call Tree.
Please also refer to our documentation on Profilers on our website.
Method List
As the name suggests, ‘Method Lists’ shows a list of methods that executed when you profiled your data, with the samples count.
Events
When you profile your applications using the Java Flight Recorder, you can also view an Events tab. It enables you to view data related to JVM events like Class loading, Garbage collection, OS events and many more.
Attaching to an existing configuration
You can also attach the profiler to a process that is in execution (including the IDE). This essentially means that you don’t need to restart your application to profile it.
Select the option ‘Attach profiler to process..’ from the Run menu and then select from the list of processes that are executing.
Using Search method in the call tree
Application profiling can generate a lot of data. To quickly navigate to the metrics for a method you are looking for, you search it in the call tree using the search option in IntelliJ IDEA Ultimate. Transitioning to the flame graph is easy too. Right-click the method name and select ‘Focus on method name in flame graph’.
Search with filtering in call tree and viewing callees and backtraces
You could easily view callees and backtraces for methods in your profiled data with a single click:
Import profiling results created by other utilities
The profiled data can be stored in a file – by IntelliJ IDEA Ultimate and other utilities. You can import profiling results created by other utilities and IntelliJ IDEA:
Opening Merge/back
Let me wrap up with what you can do with the integrated profilers in Java by sharing that you can also view ‘Method Merged Callees’ when you right-click a method name in flame graph or call list.
With out-of-the-box support for multiple profilers (Java Flight Recorder and Async Profiler), IntelliJ IDEA Ultimate makes it easier for you to profile your applications without leaving your IDE.
Happy Profiling with IntelliJ IDEA Ultimate.
Custom profiler configurations
For CPU and allocation profiling, IntelliJ IDEA provides integration with the following profilers:
- Java Flight Recorder – a standard profiling tool shipped as part of the JDK.
- Async Profiler – a very accurate profiler that can also collect native call and memory allocation data.
By default, IntelliJ IDEA runs both profilers in parallel to provide most accurate results. While it is possible to use the supported profilers separately, the combined configuration that you get out of the box is a better choice for most scenarios. This approach leverages the advantages of both profilers and abstracts you from any setup whatsoever unless you have very specific requirements.
The Async Profiler and Java Flight Recorder configurations allow you to run these tools separately and fine-tune them according to specific requirements. This topic describes how to set up these configurations and environment.
Create a separate profiler configuration
- Go to Settings | Build, Execution, Deployment | Java Profiler .
- Click Add New Configuration and select the profiler.
- Modify the profiler options as required. For the options’ meaning and purpose, refer to the Async Profiler and Java Flight Recorder chapters respectively.
Async Profiler
Async profiler is compatible with HotSpot JVM and some other Java runtimes.
On Windows and macOS, the profiler works out of the box. If you are on Linux, you have to adjust kernel options before you start profiling.
Adjust kernel options on Linux
- Adjust perf_event_paranoid . This option controls the use of the performance events data by non-root users. Set the value to be less than 2 to let the profiler collect performance information without root privileges:
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’
Configuration options
You can find the detailed explanations of Async Profiler configuration in the official documentation on GitHub.
The options that will be passed to the agent. They can be used, for example, to enable wall-clock profiling, or adjust the sampling interval.
The agent that will be used for profiling.
Java Flight Recorder
Java Flight Recorder is available on Windows, Linux, and macOS. It is supported natively in the following JDK builds:
- Oracle JDK 8-10: provided that the UnlockCommercialFeatures VM option is enabled
- Oracle JDK 11 and later
- OpenJDK 11 and later
Enable JFR for Oracle JDK 8-11
- From the main menu, select Run | Edit Configurations and from the list on the left, select the run configuration that you want to analyze with JFR.
- To the VM options field, add the following line: -XX:+UnlockCommercialFeatures . The flag unlocks commercial features that are the products Oracle Java SE Advanced or Oracle Java SE Suite. Before enabling them, read Oracle Binary Code License Agreement for the JAVA SE Platform Products carefully.
- Apply the changes and close the dialog.
JFR configurations
There are two pre-installed configurations: Default and Profile . The Default configuration has low overhead (about 1%). That’s why it works well for continuous profiling. The Profile configuration has overhead about 2% and can be used for more detailed application profiling.
These configurations cover most use cases. You can select the most suitable configuration in Settings | Build, Execution, Deployment | Java Profiler | Java Flight Recorder .
If the pre-defined configurations don’t meet your needs, you can create and upload your own settings by means of Java Mission Control .
Create a custom JFR configuration (optional)
- Run Java Mission Control that you can find in JAVA_HOME/bin/jmc or download from https://jdk.java.net/jmc/.
- From the Java Mission Control main menu, select Window | Flight Recording Template Manager.
- Click Import Files on the panel on the right and select one of the pre-installed configurations in the .jfc format:
- For Java 10 and earlier: JAVA_HOME/jre/lib/jfr/
- For Java 11 and later: JAVA_HOME/Contents/Home/lib/jfr/
- Select Edit | Advanced , change the necessary settings, and click OK .
- In the Flight Recording Template Manager dialog, click Export File .
- Specify another name for the new settings file and change its location if necessary. Click Save .
For more information on how to configure custom settings, refer to Controlling Recording Data by Using Templates.
The default JAVA_HOME path on Windows is: C:\Program Files\Java , on macOS: /Library/Java/JavaVirtualMachines .
Load the custom configuration to the IDE (optional)
- In the Settings dialog ( Ctrl+Alt+S ), select Build, Execution, Deployment | Java Profiler .
- Select the Java Flight Recorder profiling configuration to which you want to load your custom settings or create a new configuration by clicking .
- In the Profiling Settings area, select the Custom settings file option and click .
- Specify the path to the custom settings file and click Open .
- Apply the changes and close the Settings dialog.
How can I run Java Flight Recorder in the Intellij Idea community development environment with OpenJDK 16
I am interesting to run Java Flight Recorder wiht Intellij Idea Community.
I have pointed the options:
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording:filename="D:\!_git\tmp\core-java-perf\file.jfr"
But I don’t find the file -> file.jfr;
Maybe someone knows how to work with Java Flight Recorder from the Intellij Idea community?
Answer
davy.ai
- First, make sure you have Java 8 or higher installed on your system.
- Open your project in Intellij Idea Community and go to Run|Edit configurations.
- Click the “+” button in the top left corner and select “Application”.
- In the “Main class” field, select the class that you want to run Flight Recorder on.
- In the “VM options” field, enter the following code:
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording:filename="D:\!_git\tmp\core-java-perf\file.jfr"
If you still can’t find the file after running the application, try searching for it in the file explorer at the specified location. Also, make sure that Java Flight Recorder is enabled in your Java installation by checking the Java Mission Control settings.