Sub topic 3
When the JVM Accepts or Rejects an AOT Cache
Assume you can compile and run a Java JAR, use a classpath, and recognize basic JVM startup work. An AOT cache is useful only when the application and runtime match the information recorded when the cache was built.
When the JVM Accepts or Rejects a Cache
The JVM checks the cache before using it. Important inputs include:
- The JDK version and runtime build.
- The application classes and their locations.
- The classpath or module path.
- Relevant JVM options and cache metadata.
If these inputs differ, the JVM may reject the cache or use only the compatible parts. A rejected cache is usually safe: the JVM starts normally and loads classes in the usual way.
With the default settings, an unusable cache can fall back to normal startup. Use AOTMode=on when silently losing the optimization would hide a deployment error.
Creating the Cache in One Step with JDK 25
JDK 25 can record the application and create the cache in one training run:
java -XX:AOTCacheOutput=app.aot -cp app.jar Hello
java -XX:AOTMode=on -XX:AOTCache=app.aot -cp app.jar HelloThe first command produces app.aot. The second command requires that the JVM use it.
Faster Warmup with Cached Method Profiles
A cache can include method profiles collected during training. A method profile describes observed call targets and execution behavior. The JIT compiler can use this information sooner, reducing warmup time after startup.
The result depends on the training run. If production follows a different path, cached profiles may provide little benefit. They are an optimization, not a guarantee.
Building the Cache in CI and Container Images
Build the cache after compiling the exact application artifact. Put the JDK runtime, JAR files, and cache in the same container image. Do not build with one dependency set and deploy another.
A typical pipeline is:
- Build the JAR.
- Run a representative training command.
- Store the generated cache with the image.
- Start production with
-XX:AOTMode=on. - Rebuild when the JDK, dependencies, or application changes.
Limitations and Troubleshooting
A cache does not remove all startup work. It does not make application initialization, network connections, configuration loading, or unusual runtime paths free.
If startup falls back, check the JDK build, classpath order, JAR contents, JVM options, and cache path. Compare the build and runtime image contents. Use class-load logging and AOTMode=on to distinguish cache use from normal startup.
Exercise
Why should a container image rebuild its AOT cache after changing a dependency?
Show answer
The dependency changes the classes and metadata validated by the cache, so the old cache may be rejected or provide incomplete benefits.