Sub topic 2
Choosing a Representative Training Run
Assume you can compile and run Java classes, create JAR files, and use a classpath. You should also know that the JVM loads and links classes before your application can do useful work.
Choosing the Training Run
A training run is a normal application run that records startup behavior. It is not a benchmark. Its job is to exercise the classes and code paths that should be ready when the application starts.
For a command-line service, run the same command used in production. For a web service, start it, send representative requests, and then stop it. Include optional features that users commonly need. A cache built from an incomplete run may leave important classes outside the cache.
Record, Create, and Run
With JDK 24, build the cache in three steps. First, record a run. Next, create the cache from its configuration. Finally, run the application with that cache.
# Record classes used by the training run
java -XX:AOTMode=record \
-XX:AOTConfiguration=app.aotconf \
-cp app.jar Hello
# Create the cache
java -XX:AOTMode=create \
-XX:AOTConfiguration=app.aotconf \
-XX:AOTCache=app.aot \
-cp app.jar
# Use the cache
java -XX:AOTCache=app.aot -cp app.jar HelloThe record step must use a representative command. The create step reads the recorded configuration and writes app.aot. The final command still loads the application from app.jar; the cache supplies reusable startup data.
JDK 25 also supports one-step creation:
java -XX:AOTCacheOutput=app.aot -cp app.jar HelloMeasuring the Improvement
Run each version several times and compare wall-clock time. The first run may include operating-system file-cache effects, so discard obvious outliers.
time java -cp app.jar Hello
time java -XX:AOTCache=app.aot -cp app.jar Hello
java -XX:AOTCache=app.aot -Xlog:class+load -cp app.jar Hello | grep HelloThe class-load log helps confirm cache use. Cached classes commonly show shared objects file as their source. If the cache is missing, incompatible, or unusable, the JVM may continue without it. Use -XX:AOTMode=on when a fallback would hide a deployment error.
Exercise
Why should a training run exercise the same startup path that production uses?
Show answer
The run records the classes and behavior the cache can reuse. An unrepresentative run can omit classes needed in production.