Sub topic 1
Where JVM startup time goes
Prerequisites
This chapter assumes you can compile and run a Java class or JAR, use a classpath, and run shell commands. You should also know that the JVM uses just-in-time (JIT) compilation to optimize frequently executed code while an application runs.
Where JVM startup time goes
A Java process performs several tasks before main can do useful work:
- The operating system starts the process and the JVM initializes its runtime.
- The JVM loads classes from the classpath and JDK modules.
- It verifies class-file safety and links symbolic references.
- It creates objects such as class metadata, method handles, and interned strings.
- The application initializes frameworks, configuration, logging, and dependency injection.
The JIT compiler usually does not dominate the first few milliseconds. It improves code after the application has run long enough to collect execution data. Short-lived programs often finish before JIT warmup provides much benefit.
Class loading, verification and linking
A class loader locates a class definition and creates the JVM's internal representation of it. Verification checks that the bytecode follows JVM safety rules. Linking prepares symbolic references, such as a method call referring to a method in another class.
These steps are necessary, but many startup runs repeat them with the same JDK and application classes. The JVM must also resolve commonly used constants and create some objects before application code can use them.
From Class Data Sharing to the AOT cache
Class Data Sharing (CDS) lets multiple JVM processes reuse preprocessed class data from a shared archive. AppCDS extends this idea to application classes. The archive reduces repeated class loading and linking work.
The AOT cache builds on CDS. A training run records the classes and startup behavior an application actually uses. A later run maps the resulting cache and reuses prepared data instead of rebuilding it from class files.
What the AOT cache stores
The cache can contain prepared class metadata, resolved linking information, archived heap objects, and other startup data derived from the training run. It is tied to the application, JDK, platform, and relevant runtime options. A cache is therefore a deployment artifact, not a universal Java optimization.
| Stored or reused data | Startup benefit |
|---|---|
| Loaded class metadata | Less class-file processing |
| Resolved links and constants | Less repeated linking |
| Archived startup objects | Fewer early allocations |
Exercise
Why might an AOT cache provide little benefit to an application that spends most of its startup time waiting for a database?
Show answer
The cache can reduce JVM class and linking work, but it cannot remove the database connection or other external wait.