Sub topic 17
Beyond C1 and C2: AOT and Graal in JDK 11
This chapter assumes you understand Java bytecode, the JVM, and HotSpot's C1 and C2 compilers. The default JDK 11 HotSpot VM uses tiered compilation with C1 and C2. Ahead-of-time (AOT) compilation and Graal offer alternatives, but they are not drop-in replacements for every application.
Ahead-of-time compilation in JDK 11
AOT compilation produces native machine code before the application starts. The JVM can load that code instead of compiling the same classes during startup. This can reduce startup work and may reduce warmup time.
JDK 11 includes the experimental jaotc tool on supported platforms. It uses the Graal compiler to produce a native shared library from Java modules or classes.
# Compile the java.base module into a native library
jaotc --output libjava.base.so --module java.base
# Run with the generated AOT library
java -XX:AOTLibrary=./libjava.base.so -versionAOT code is tied to details such as the operating system, CPU architecture, JDK build, and class layout. You must regenerate it when those details change. AOT also does not remove all runtime work. The JVM still loads classes, performs checks, interprets code that was not compiled, and may use JIT compilation for code that benefits from runtime information.
AOT is most useful when startup time matters and the workload is predictable. It is less attractive for long-running services where C2 eventually learns the real workload and produces better specialized code.
Graal as an alternative JIT compiler
Graal is a compiler written in Java. It can operate through the JVM Compiler Interface, or JVMCI. In JDK 11, Graal can replace C2 as the high-tier compiler when the required Graal components are present and experimental options are enabled.
java \
-XX:+UnlockExperimentalVMOptions \
-XX:+UseJVMCICompiler \
-versionWith this configuration, C1 can still provide quick compiled code while Graal handles more aggressive optimization. Graal uses profiling data, inlining, speculative assumptions, and deoptimization in the same general way as C2. Its internal algorithms and optimization pipeline differ from C2, so performance depends on the workload.
Graal is also the foundation of GraalVM, a separate distribution that adds language runtimes and native-image tooling. The Graal compiler available in a particular JDK 11 build may not match the version or features in a GraalVM release.
Choosing between the options
| Choice | Main benefit | Main cost |
|---|---|---|
| C1 and C2 | Mature default behavior | Startup and warmup work |
| AOT | Less startup compilation | Portability and limited adaptation |
| Graal JIT | Alternative optimization pipeline | Extra configuration and testing |
Use the default tiered compiler unless measurements show a problem. Consider AOT for repeatable startup-sensitive workloads. Consider Graal when you can benchmark it on the target hardware and its optimization behavior fits the application.
Exercise
Why might AOT code be worse than JIT-compiled code for a long-running service?
Show answer
AOT code is produced before the real workload runs, so it cannot use the application's runtime profiling data as effectively as a JIT compiler can.