How to profile my code

I’d like to understand the use model of code profiling my Scala program using IntelliJ. I have found a Profiler menu item in the IntelliJ menus. But as far as I can tell it doesn’t do anything. Do I need to install Jprofiler? Do I need to compile my program with any particular debug flags to make it profilable?

Ideally I’d like to know how much time is being spent on each line or each expression of the Scala program in question. Is that the idea? Or are my expectations completely wrong?

That’s probably this feature. The pedestrian way of profiling on the JVM is hprof.

Looks like the profiler is not installed by default because it is experimental on MacOS. Even the instructions on the web page seem to be out of date. It explains that I need to enable java.profile.enabled in the Experimental Features dialog.


But sadly no such feature seems to exist. I’ll inquire with IntelliJ support.

Turns out the profiler in IntelliJ is only available in the “Ultimate” edition. :frowning:

@sangamon, what do I need to do to be able to run the profiler from the command line? One of the links your referred me to suggests the following:

java -agentlib:hprof TheClassName

But I suspect there is more boiler plate necessary:

jimka> java -agentlib:hprof DimacsSuite
Error: Could not find or load main class DimacsSuite
Dumping Java heap ... allocation sites ... done.
jimka> java -agentlib:hprof dimacs.parseDimacs
Error: Could not find or load main class dimacs.parseDimacs
Dumping Java heap ... allocation sites ... done.

For IntelliJ, edit the launch configuration for the main class you want to profile and add something like


to VM Options.

For sbt, add

fork in run := true
javaOptions in run += "-agentlib:hprof=cpu=samples,depth=8"

to your build.sbt.

In both cases, running your main class should generate a file java.hprof.txt in your project root folder then.

Don’t expect miracles. This is pretty basic and doesn’t cater to Scala specifics.

1 Like

I can’t say anything regarding the profiler, but as someone at a university you can get IDEA Ultimate for free: (includes not only students but also faculty staff)

1 Like

wow, that’s great. I applied as soon as I read your message. Let’s see if my private French engineering school qualifies for their program.

A few years ago I was doing some JVM profiling and at the time a lot of the sources I found recommended VisualVM. It used to be standard with the JDK download. Looks like it went away after Java 8, but you can still get it at The biggest downside was that it was a GUI that you needed to connect to a process to profile. This is fine for long-running programs like servers, but it doesn’t work for short scripts. On the plus side, the VisualVM seemed to have a much lower performance impact than hprof. My memory is that running my program under hprof made it go something on the order of 10x slower than a normal run but connecting the VisualVM caused less than a 2x performance hit.

A really great option is to use async-profiler as a Java agent (like the hprof example above, but with a much smaller effect on the performance of the application).

You can download it here. Then you pass the path to the profiler shared library when you launch your program:

java -agentpath:/path/to/,svg,file=profile.svg DimacsSuite

This will produce an interactive SVG that you can open in your browser. You can also use this approach with the IntelliJ launch configuration or the SBT configuration @sangamon posted.

Note that the error you pasted above is because you have provided no class path to the java process so it does not know where to find the class DimacsSuite. The class name should be fully qualified too (e.g. if it’s in package dimacs, then dimacs.DimacsSuite).


java -agentpath:/path/to/,svg,file=profile.svg -cp /path/to/classfiles dimacs.DimacsSuite

If you get IntelliJ Ultimate it will do all of this setup for you. :slight_smile:

1 Like

I downloaded the Ultimate edition of IntelliJ and tried again to follow the instructions for the profiler. But there seems to still be a problem. in the mean time I filed an issue at the JetBrains issue tracker. The issue was subsequently triaged without any commentary. Anyone have any idea what that means?

I think it just means some Jetbrains employee (or some bot) has viewed the bugreport and assigned it to someone working on the profiler code.