Sub topic 6
Record, create and run
This chapter walks through the three-step workflow that builds and uses an AOT cache on JDK 24 or later. It assumes you can compile a class, package a JAR, and run java -cp from a shell. You should also know roughly what class loading and linking do at startup.
The idea in one pass
The AOT cache moves class loading and linking work out of every run and into a one-time build step. To build it, the JVM needs to know which classes your application uses. It learns this by watching one run, called the training run. The workflow has three steps:
- Record. Run the application once. The JVM writes the list of classes it used to a configuration file.
- Create. Run the JVM again in a mode that reads the configuration file and writes the cache file.
- Run. Start the application with the cache file.
A sample application
The program below uses streams, lambdas and method references. These features pull in many JDK classes at startup, so they make a good subject for the cache.
import java.util.List;
import java.util.stream.Collectors;
public class Hello {
public static void main(String[] args) {
List<String> names = List.of("ada", "grace", "linus");
String result = names.stream()
.map(String::toUpperCase)
.collect(Collectors.joining(", "));
System.out.println(result);
}
}Compile it and package it as a JAR:
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.classStep 1: Record
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello-XX:AOTMode=record tells the JVM to observe this run. -XX:AOTConfiguration names the file where it writes what it saw. The program runs normally and prints its output. When it exits, app.aotconf holds the list of classes that were loaded and linked.
This run is the training run. The cache will contain only what this run touched, so a run that skips a code path gives you no benefit on that path later. Chapter 5 covers how to choose a representative run. For now, the sample program exercises everything it needs.
Step 2: Create
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jarThis step does not run your application. There is no main class on the command line. The JVM reads app.aotconf, loads and links the listed classes, and writes them to app.aot in a form it can map into memory quickly.
Keep the -cp value the same as in the record step. The cache is tied to the classpath it was built with, and the JVM checks this when it starts. The rules for when a cache is accepted are covered in a later chapter.
Step 3: Run
java -XX:AOTCache=app.aot -cp app.jar HelloThe only change from a normal launch is -XX:AOTCache. The JVM maps app.aot and takes the cached classes from it instead of reading, parsing, verifying and linking each one again. The output is the same as before: ADA, GRACE, LINUS.
The three commands together:
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
# 1. Record: run the app once and write a configuration file
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello
# 2. Create: build the cache from the recorded configuration
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jar
# 3. Run: start the app using the cache
java -XX:AOTCache=app.aot -cp app.jar HelloWhich flag does what
| Flag | Used in | Meaning |
|---|---|---|
| `-XX:AOTMode=record` | Record | Observe the run and collect class usage |
| `-XX:AOTMode=create` | Create | Build the cache from a configuration file |
| `-XX:AOTConfiguration=file` | Record, Create | The intermediate file that links the two steps |
| `-XX:AOTCache=file` | Create, Run | The cache file that is written, then read |
Common mistakes
- Changing the classpath between steps. If the JAR path or its contents differ between create and run, the JVM cannot use the cache.
- Rebuilding the JAR and keeping the old cache. Any change to the JAR makes the cache stale. Repeat all three steps after each build.
- Using different JDK builds. Build and use the cache with the same JDK. A cache from another build is not valid.
- Expecting an error when the cache is unusable. By default the JVM runs without the cache and may print only a warning. A later chapter shows how to make this fail loudly.
Exercise
You build a cache for app.jar, then recompile Hello.java and repackage app.jar. You start the app with the old app.aot. What should you do, and which step produces the file that you pass to -XX:AOTCache at run time?
Show answer
Repeat the record and create steps against the new JAR, because the old cache no longer matches it. The create step (-XX:AOTMode=create) writes the cache file, app.aot, that the run step reads with -XX:AOTCache.