This demo shows how to create a native executable with GraalVM Native Image and apply Profile-Guided Optimization (PGO) for more performance gains. It uses the Java application that counts files and their sizes in a specified directory.
-
Compile the application:
javac ListDir.java
-
Generate a native executable from the class file:
native-image ListDir
The executable file listdir is created in the directory.
-
Run the application by passing it a directory that actually contains some files, for example
..
(to count files in the parent of the current directory, containing all the demos in this repository). Check the time spent with thetime
utility.- On HotSpot:
time java ListDir ..
- From a native executable:
time ./listdir ..
- On HotSpot:
For more performance gains, you can can apply Profile-Guided Optimization (PGO). (Not available with GraalVM Community Edition.)
With PGO you can collect the profiling data, and then feed it to the native-image
tool, which will use this information to further optimize the performance of the resulting executable.
-
Build an instrumented image from the ListDir class and run it to collect profiles, specifying a different name for the native executable, for example listdir-instrumented:
native-image --pgo-instrument ListDir -o listdir-instrumented
./listdir-instrumented ..
Profiles collected from this run are now stored in the
default.iprof
file. -
Use the profiles to build an optimized native executable, giving it a different name than in the previous runs:
native-image --pgo ListDir -o listdir-optimized
-
Run that optimized executable:
time ./listdir-optimized ..
Find more examples at the website: