Sub topic 6
Record, create and run
This chapter assumes you can compile a class and package it into a JAR, you know what the classpath is, and you have a rough picture of class loading from the earlier chapters. You need JDK 24 or later on your path. Check with java -version.
The workflow at a glance
The AOT cache is built in two phases before the application ever runs with it. The first phase watches a training run and writes a configuration file. The second phase turns that file into the cache. After that, every normal start can load classes from the cache and skip the parsing, verification and linking work that each run would otherwise repeat.
The configuration file app.aotconf is an intermediate product. Only app.aot is needed at run time.
Preparing the application
The examples use a small program that relies on streams, lambdas and method references. These features pull in many JDK classes during startup, so the cache has a lot to work with.
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 into a JAR. The cache is tied to the classpath, so we use a JAR from the start, as a real deployment would.
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.classStep 1: record
In the record step you run the application once, with the JVM told to observe itself.
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello-XX:AOTMode=record switches the JVM into observation mode. -XX:AOTConfiguration=app.aotconf names the file to write. The application behaves as usual and prints ADA, GRACE, LINUS. When the JVM exits, it writes the list of classes that were loaded and linked, along with some information about how they were used.
Three details matter here:
- The JVM writes the file at exit. A normal return from
mainor a call toSystem.exitis fine. A process killed withkill -9or one that crashes leaves you without a usable file. - The run is slower than normal because the JVM is collecting data. Never use timing from the record step as a benchmark.
- The recorded content is only as good as the training run. A class that the run never touched will not be in the configuration. The next chapter covers how to design that run. For now, remember that the cache describes what you did, not what the program could do.
Step 2: create
The create step reads the configuration and builds the cache.
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jarNote what is missing: there is no main class on this command line. The JVM does not run your application in this step. It loads and links the recorded classes from the classpath, then writes the result, in a form that can be mapped into memory later, to app.aot. The file is larger than you might expect for a tiny program, often tens of megabytes. It contains JDK classes as well as your own, because the cache is a self-contained image rather than an addition to the default JDK archive.
Because the create step reads your JARs again, they must be the same ones used for recording. If you rebuild app.jar between steps, the JVM can detect the change and refuse to build the cache, or it can build a cache that later turns out to be unusable. Treat the record and create steps as a pair that always run back to back against the same artifacts.
Step 3: run
Starting the application with the cache needs one extra flag.
java -XX:AOTCache=app.aot -cp app.jar HelloThe output is identical. Only the startup path changes. The JVM maps app.aot into memory and takes the classes from it, instead of finding each one on the classpath, parsing it, verifying its bytecode and linking it.
The JVM does not fail if something is wrong with the cache. With the default setting, -XX:AOTMode=auto, a missing or unusable cache is skipped and the application runs the slow way. That is a safe default, but it also means a broken cache can go unnoticed. The measuring chapter shows how to confirm that the cache is in use, and the one-step chapter shows how to make a bad cache a hard error with -XX:AOTMode=on.
Which options belong to which step
Some flags are tied to a step and others have to stay consistent across all three. The table summarizes the flags used in this chapter.
| Flag | Record | Create | Run | Purpose |
|---|---|---|---|---|
| `-XX:AOTMode` | `record` | `create` | optional (`auto` by default) | Selects the step |
| `-XX:AOTConfiguration` | output file | input file | not used | Holds the recorded class list |
| `-XX:AOTCache` | not used | output file | input file | The cache itself |
| `-cp` | required | required | required | Must be compatible across steps |
| Main class | required | omitted | required | Only record and run execute the program |
Rules that tie the steps together
The cache is a snapshot of one JVM and one classpath. It is only valid when the run-time environment matches the environment in which it was built.
- Same JDK build. The cache records internal layouts of the JVM. Use the same JDK vendor, version and platform for all three steps. A cache built with one JDK build will not load in another, and an update to the JDK means rebuilding the cache.
- Compatible classpath. The run-time classpath must start with the same entries, in the same order, as the record-time classpath. You may add entries at the end. You may not reorder or remove entries. The JARs must also be unchanged, which the JVM checks using file size and modification time.
- Same options that affect class layout. Some options change how classes are loaded or laid out, such as module-related options and certain GC or memory flags. If they differ, the JVM can disable parts of the cache or reject it. Keep the JVM options in all three steps the same except for the ones that belong to a step.
- Classes from directories. Classes loaded from a plain directory on the classpath cannot be cached, so package your code as JARs.
Checking that it worked
A faster run is a hint but not proof. Two quick checks give a better answer. First, compare wall-clock times:
time java -cp app.jar Hello
time java -XX:AOTCache=app.aot -cp app.jar HelloFor a tiny program the difference is a few tens of milliseconds, so repeat each command several times and compare the typical value rather than a single run. Second, ask the JVM where each class came from:
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloThe log line for Hello should end with source: shared objects file. Without the cache you see the path of app.jar there instead. Look at a JDK class, such as java.util.stream.Collectors, as well. If your own class comes from the JAR but JDK classes come from the shared archive, the JVM is using only its default archive and not your cache.
Automating the three steps
Once the commands work by hand, put them into a script so that they always run together against the same build output. This sketch uses plain shell:
#!/bin/sh
set -eu
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
rm -f app.aotconf app.aot
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar Hello
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -cp app.jar
ls -l app.aotset -eu stops the script at the first failing command. Without it, a failed record step would lead to a confusing error in the create step. Deleting old outputs first means a stale cache from an earlier build cannot be mistaken for a fresh one.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No `app.aotconf` after the record step | The process was killed or crashed before exit | Let the training run finish normally |
| Create step reports a problem with the classpath or a JAR | `app.jar` changed between record and create | Rebuild the JAR first, then run both steps again |
| Run step works but is not faster | The cache was silently rejected | Check with `-Xlog:class+load` and look for warnings about the cache |
| Cache stops working after a JDK upgrade | The cache belongs to the older JDK build | Rebuild the cache with the new JDK |
| Only some classes load from the cache | The training run did not use them | Extend the training run to cover them |
| Cache stops working after moving files to another host | Paths, timestamps or JDK differ | Build the cache where the application runs, or copy exactly the same JDK and JARs |
Exercise
Exercise
You run the record step, then change one line of Hello.java, rebuild app.jar, and run the create step with the old app.aotconf. Then you start the application with -XX:AOTCache=app.aot and see no speedup. Name the likely cause, and say what you should have done instead.
Show answer
The configuration was recorded against the old app.jar, and the JAR it describes no longer matches the one on the classpath, because its size and modification time changed. The JVM either refused to build a cache or the cache is rejected at run time, and with the default AOTMode=auto the application then starts without it. The record and create steps must run against the same, unchanged JAR. After any rebuild, repeat the record step first, then create, then run.
Exercise
Why does the create command have no main class, and why does the record command need one?
Show answer
The record step runs the application so the JVM can observe which classes it loads, so it needs the main class. The create step does not run the application. It only loads and links the classes listed in the configuration file and writes the cache, so a main class is not needed.