Sub topic 5
Choosing a representative training run
This chapter assumes you can compile a class and package a JAR, you know what the classpath is, and you have a rough idea of how the JVM loads classes and compiles hot methods. You should also be comfortable timing shell commands.
What the training run is
The AOT cache is built from a record of what one real execution of your program did. That execution is the training run. In the record step, the JVM watches which classes get loaded and linked, and it writes them to a configuration file. The create step then turns that file into the cache.
The cache can only hold what the training run saw. The JVM does not guess which classes you might need later. Choosing the training run therefore decides which classes load from the cache at production startup and which load the slow way.
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);
}
}Here the training run is the whole program, so every class it touches is recorded: the stream classes, the lambda and method-reference machinery, and Collectors. The record command is the one from the workflow chapter:
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -cp app.jar HelloWhat a good training run looks like
A good training run behaves like production startup, with the same code paths, the same arguments and the same classpath. Use these rules.
- Run the real entry point. Start the application through its normal main class, not a test harness that wraps it.
- Exercise the startup path fully. Initialize frameworks, read configuration, open the connections or handlers the app creates at launch, and serve a first request if the app is a server.
- Include the first use of common features. If most requests hit JSON parsing, logging and a database driver, call each of them once. Their classes are then recorded.
- Let the run exit cleanly. The configuration file is written when the JVM exits. A run that is killed may not produce a usable file, so give the application a way to stop itself or to shut down normally.
- Keep it short and repeatable. A training run that takes minutes or depends on external state makes the build fragile.
A run that loads too little leaves classes out of the cache. A run that loads rarely used classes only makes the cache larger, and the cost is usually small. When unsure, lean toward covering more.
Where training goes wrong
Most problems come from a mismatch between the training run and production.
| Mistake | Effect |
|---|---|
| Training with a different classpath than production | The cache is rejected or its classes are not used |
| Training only `--help` or a trivial path | Few classes recorded, little speedup |
| Training with a different JDK build | The cache does not validate |
| Training a run that crashes before exit | No usable configuration file |
| Skipping a feature that production uses at startup | Those classes load the slow way |
The classpath point matters most. The JVM compares the classpath used at run time with the one recorded, so build the cache with the same JAR paths you deploy. A later chapter covers the exact validation rules.
Diagram: coverage decides what is cached
A missing class is never an error. It loads the usual way and the program still works. Poor coverage costs speed and nothing else.
Checking coverage
You can see what your training run missed. Run production with the cache and log class loading:
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar HelloClasses from the cache report a shared objects file as their source. Classes loaded from a JAR report the JAR path. If many application classes show a JAR path, add the missing behavior to the training run and rebuild the cache.
Exercise
Exercise
Your service takes 4 seconds to start. You build an AOT cache by training with java -XX:AOTMode=record ... App --version, which prints the version and exits. Startup improves only slightly. Why, and what should you change?
Show answer
The --version path loads very few classes, so the configuration file records almost none of the classes the service needs at normal startup, such as its framework, server and logging classes. Those still load from the JAR. Change the training run to start the service through its normal entry point, exercise the startup path and a few typical requests, and then shut it down cleanly so the configuration is written. Then recreate the cache and check with -Xlog:class+load that application classes now report a shared objects file as their source.