Table of Contents

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 OptimizedImage.java 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<>();

    languages.add("JavaScript");
    languages.add("Python");
    languages.add("Ruby");

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

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

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:

./optimizedimage

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
./optimizedimage

You can specify where to write the profile when running an instrumented native image by passing the -XX:ProfilesDumpFile=YourFileName option at run time. You can also collect multiple profile files, by specifying different names, and add them to the image build.