Profile-Guided Optimizations

GraalVM Enterprise allows to apply profile-guided optimizations (PGO) for additional performance gain and higher throughput of native images. With PGO you can collect the profiling data in advance and then feed it to the native image builder, which will use this information to optimize the performance of the resulting binary.

Note: This feature is available with GraalVM Enterprise only.

Here is how you can build an optimized native image, using the example program.

1. Save this Java program that iterates over ArrayList using a lambda expression to a file and compile it:

import java.util.ArrayList;

class OptimizedImage {
  public static void main(String[] args) {
    ArrayList<String> languages = new ArrayList<>();


    System.out.print("ArrayList: ");

    languages.forEach((e) -> {
      System.out.print(e + ", ");

2. Build an instrumented native image by appending the --pgo-instrument option, whose execution will collect the code-execution-frequency profiles:

native-image --pgo-instrument OptimizedImage

3. Run this instrumented image, saving the result in a profile.iprof file, if nothing else is specified:


4. Lastly, create the second native image by specifying the path to the profile.iprof file and execute it.

native-image --pgo=profile.iprof OptimizedImage

You can collect multiple profile files, by specifying different names, and add them to the image build.