Sub topic 17
Beyond C1 and C2: AOT and Graal in JDK 11
This chapter assumes you have read the earlier chapters on tiered compilation. You should know what C1 and C2 do, what tiers 0 to 4 mean, and how to read -XX:+PrintCompilation output. It covers two experimental features that JDK 11 added to HotSpot. One is ahead-of-time (AOT) compilation with jaotc. The other is the Graal compiler, which can replace C2. Neither feature is part of the default configuration, and both were later removed.
The problem these features address
Tiered compilation has two costs that earlier chapters described:
- Startup and warmup. Every program begins in the interpreter. Code reaches full speed only after counters trigger compilation and profiling finishes.
- C2 is written in C++. It is fast and heavily tuned, but it is hard to extend and to experiment with.
JDK 9 to 11 explored two answers. AOT moves some compilation before the program runs. Graal replaces a compiler written in C++ with one written in Java.
AOT compilation with jaotc
JEP 295 added the jaotc tool to JDK 9, and it ships in JDK 11. It compiles class files into a shared library (a .so file) that contains native code. At run time, the JVM loads that library and uses the precompiled code instead of interpreting those methods.
In JDK 11, AOT is supported only on Linux x64, and it is marked experimental.
Compiling and running
Use the HotLoop class from earlier in this tutorial:
javac HotLoop.java
# Compile the class into a native library
jaotc --output libHotLoop.so HotLoop.class
# Run with the library loaded
java -XX:AOTLibrary=./libHotLoop.so HotLoop
# Confirm that AOT code is being used
java -XX:AOTLibrary=./libHotLoop.so -XX:+PrintAOT HotLoopWith -XX:+PrintAOT, the JVM prints a line when it loads each AOT library. -XX:+PrintCompilation marks methods that come from AOT code with an A column entry, so you can see which methods skipped the interpreter.
How AOT code interacts with tiering
AOT code does not replace the tiered pipeline. It is a starting point inside it:
- A precompiled method runs as native code from its first call, so it skips tier 0.
- The AOT code is not as well optimized as C2 output.
jaotchas no run-time profile, and it cannot assume things such as class loading state. - The JVM still counts invocations. A hot AOT method is recompiled by C1 and then by the top-tier compiler, as an interpreted method would be.
- By default, AOT code carries no profiling instrumentation. The
jaotcoption--compile-for-tieredadds it, so the method can collect profile data before the recompile.
So AOT mainly improves startup. Steady-state peak performance still comes from the JIT.
Limits you will run into
- Exact class match. The library stores a fingerprint of each class. If the class file at run time differs from the one you compiled, the JVM ignores that class's AOT code. You must rebuild the library after every change.
- No dynamic features.
jaotccannot compile lambdas and other code that relies oninvokedynamic. Methods that use them stay in the normal pipeline. - Garbage collector. The library records the collector it was built for. Running with a different collector is not supported, so build and run with the same one.
- Maintenance cost. You now have an artifact to build, ship, and keep in sync with your application.
Graal as a JIT compiler
Graal is a compiler written in Java. JEP 317 (JDK 10) made it usable inside HotSpot through the JVMCI (JVM Compiler Interface). JVMCI lets a Java program act as a compiler plug-in. JDK 11 includes Graal in the jdk.internal.vm.compiler module on Linux x64.
Turning it on
java -XX:+UnlockExperimentalVMOptions \
-XX:+EnableJVMCI \
-XX:+UseJVMCICompiler \
-XX:+PrintCompilation HotLoopThe three flags have different jobs:
| Flag | Purpose |
|---|---|
| `-XX:+UnlockExperimentalVMOptions` | Allows the other two flags, which are experimental. |
| `-XX:+EnableJVMCI` | Starts the JVM compiler interface. |
| `-XX:+UseJVMCICompiler` | Uses the JVMCI compiler, Graal by default, in place of C2. |
With these flags, tier 0 and the C1 tiers work as before. Graal takes over the top tier from C2. It consumes the same profile data that C1 collects at tier 3.
What changes in practice
- Graal compiles itself first. Graal is Java code, so it begins in the interpreter and must be compiled like any other code. Early compilations are slower, and it allocates objects on the Java heap, which adds some memory pressure. This warmup cost matters for short programs.
- Different optimizations. Graal has strong escape analysis and inlining heuristics. Some programs, especially ones with many small objects or streams, run faster than with C2. Others run slower. Measure your own workload.
- Different diagnostics. C2-specific flags may not apply. Graal has its own options, set with
-Dgraal.*system properties. - Experimental status. In JDK 11 it receives limited testing compared with C2, so avoid it for production unless you have measured and tested it yourself.
Choosing between the options
| Goal | Option | Trade-off |
|---|---|---|
| Best default behavior | C1 and C2 tiered | Slow warmup on short runs |
| Faster startup of a fixed application | `jaotc` library | Fragile, Linux x64 only, rebuild on every change |
| Experiment with a Java-based top tier | Graal via JVMCI | Own warmup cost, experimental |
| Smallest and fastest startup, no JVM | GraalVM Native Image | A separate product and a different runtime model, outside this tutorial |
What happened afterward
Both features saw little adoption in the OpenJDK distribution. JEP 410 removed the experimental AOT compiler and the JVMCI-based JIT interface from OpenJDK in JDK 17. The jaotc tool no longer exists after that release. Graal as a JIT continues as part of the separate GraalVM distribution. For work on a current JDK, class data sharing (CDS) is the supported way to improve startup. Treat the material in this chapter as knowledge for maintaining JDK 11 systems and for understanding how the pipeline can be extended.
Exercise
A team builds a jaotc library for their service, deploys it, and later rebuilds the service with a small code change but forgets to rebuild the library. What happens at run time, and how can they detect it?
Show answer
The JVM compares each class's fingerprint with the one stored in the library. The changed classes no longer match, so the JVM ignores their AOT code without raising an error. Those methods start in the interpreter and go through the normal tiers, so startup gets slower. The team can detect this with -XX:+PrintAOT, and with -XX:+PrintCompilation, which shows that those methods are not loaded from AOT code. The fix is to rebuild the library whenever the classes change.
Exercise
In JDK 11 with -XX:+UseJVMCICompiler, which part of the tiered pipeline does Graal replace, and why can short programs run slower with it?
Show answer
Graal replaces C2 at the top tier. Tiers 0 to 3 still use the interpreter and C1. Graal is written in Java, so it must be interpreted and compiled itself before it compiles quickly. That warmup cost and its heap allocations can outweigh its optimizations in a short program.