Education Features General IntelliJ IDEA

5 Best Features for Profiling Java Code In IntelliJ IDEA

When it comes to apps functioning efficiently, there is always room for improvement. Fast, lag-free, smooth operation – what else can a Java developer dream of?

However, when you aim for this level of perfection in your app, you will need a comprehensive toolset to monitor performance and identify problems for further analysis. That’s where profiling tools come in handy. IntelliJ IDEA has tight integration with several of the most popular Java profilers that you can run on your project without leaving the IDE.

First things first

We advise that you get acquainted with the supported Java profiling tools with a general overview. The dedicated page describes:

  • How to begin profiling
  • What views to use for analyzing the profiling results
  • How to share these results with your team

Also, it includes an introductory video that helps you get acquainted with the tools, demonstrates the configurations and various ways to run all the available profilers, and how to interpret the results.

Top 5 supported features

We’ve already published some useful materials and how-to’s that can help you get started working with the integrated profilers. In this blog post, we’ve gathered together some of the most prominent features that will let you use our integrated profilers efficiently.

Easy run – fast results

Irritating issues, such as spikes in memory consumption, require troubleshooting as fast as possible. Fortunately, in IntelliJ IDEA you can start detecting such problems by running profilers with just one click. You can choose one of two equally simple ways to do so:

  • It is possible to run supported profilers by clicking the Run button in the gutter and selecting Profile with …: Profiler start
  • If you work in the Profiler tool window, you can pick the desired process, click on it, and select the profiler you want to attach:Profiler start 2

    Once you initiate the profiling process, you can stop it at any moment. The IDE will display the profiling results immediately and start analyzing the problems right away.

    Profiling resulta

    Revising the existing .jfr and .hprof files is very simple too. To open any file of these types quickly, drag and drop it in the editor, open it with File | Open, double-click it in the Project view, or select it among the Recent snapshots listed in the Home tab of the Profiler tool window.

    Results visualization

    We provide a range of tools that allow you to thoroughly analyze the profiling results that you get after attaching the profiler to a running process, including a flame graph, call tree, method list, timeline, and events.

    The flame graph is probably the most informative and convenient way to interpret your Java program’s CPU usage. Each rectangle stands for a stack frame. Yellow ones represent Java code, while blue ones represent native method invocations. The more time a stack frame takes, the wider the dedicated rectangle becomes on the flame graph. This allows you to evaluate CPU usage and memory resources easily.

    The flame graph comes with some options that simplify working with it. For example, once you hover a mouse over each stack frame, you see a tooltip that shows a method name, percentage of the parent sample time, and percentage of total sample time.

    Profiler tooltip

    It is also possible to zoom the graph with the help of the plus and minus icons and drag it to move to the desired stack frame.

    Profiler zoom

    Among other handy options is fast navigation to a particular method on the flame graph. Just start typing its name in the search bar, and the IDE will highlight the relevant results. You can easily browse through all of them with the help of the up and down arrows. There is also a filter that allows you to search the whole flame graph or just in a specific subtree.

    When you find the necessary method, you can review it in the editor. Just right-click on it and select Jump to Source.

    .jfr snapshots’ comparison

    Starting from our recent EAP for IntelliJ IDEA 2021.3, you can compare .jfr snapshots right from the IDE. Why might you need this? It can be helpful to check whether the applied changes improve or degrade your app’s performance. This comparison is very convenient because it provides quick results and lets you make a fast yet reasonable decision to roll back the changes if your code improvement theory proves wrong.

    To compare the .jfr snapshots, open two snapshots and navigate to the one you’ve taken after applying your changes. In the top-right corner of the top pane, click the diff icon and select the snapshot you’ll use as the baseline. You will then see the result in the form of a flame graph, call tree, or method list.

    Profiler compare

    It is an experimental feature, but we feel it could come in handy in the overall profiling workflow. If you have any thoughts on making it work better for your needs, please share your feedback.

    Combined power of the CPU and Allocation profilers

    IntelliJ IDEA has support for Async Profiler 2.0. When you decide to profile your Java code with Async Profiler, it means that you simultaneously attach the CPU and Allocation profilers to the running process. And so, you combine the power of the two by clicking just one button!

    Profiler _ Async

    After you run your program with Async Profiler, the results in the Flame Graph, Call Tree, and Method List tabs are available both for CPU samples and memory allocations. To choose between the two, go to the new Show dropdown list and select the necessary option.

    Profiler Async

    The Timeline displays both of them. You can filter what to show by using the controller in the top right-hand corner.

    Profile configurations

    In IntelliJ IDEA it is possible to attach the profiler to the most common run configurations, including local Maven and Tomcat run/debug configurations. To profile them, select the necessary configuration, click the clock icon in the top toolset pane, and select the desired profiling option: Async Profiler or Java Flight Recorder.

    Profiler_config

    These were the most useful and powerful features that IntelliJ IDEA offers for profiling Java applications. We recommend you try them out and share your feedback in the comments or in the issue tracker. It will help us better support profiling tools and make your program’s memory consumption more efficient.

    Happy developing!

    Discover more