Sub topic 1
Where JVM startup time goes
This chapter breaks a JVM launch into phases. You will see which phases the AOT cache shortens and which it leaves alone. Later chapters build and measure the cache itself.
Prerequisites
This chapter assumes you can do the following:
- Compile a class and run it from a JAR with
java -cp. - Explain what the classpath is.
- Describe, roughly, what class loading and JIT compilation are.
- Run a shell command and time it with
time.
What "startup" means
Startup is the time from running java until your application does its first useful work. For a short command-line tool, that is most of the run. For a server, it is the delay before the first request is served.
The JVM does not run your bytecode at full speed from the first instruction. It prepares a runtime, loads and checks every class it needs, and only then runs code. At first it interprets that code slowly, and later it compiles the hot parts.
The phases
- JVM initialization. The process starts, the heap and garbage collector are set up, and the core JDK classes become available. This cost is small and mostly fixed.
- Class loading. A class loader finds the bytes of a class, such as a
.classentry in a JAR, and parses them into the JVM's internal representation. Your own code, your libraries, and the JDK classes they use all go through this step. - Linking. Linking has three parts. Verification checks that the bytecode is safe. Preparation allocates static fields. Resolution turns symbolic references, such as the name of a method, into direct ones. The next chapter covers these parts.
- Class initialization. Static initializers run, and then
mainruns. - Warmup. The interpreter runs the code first. The JIT compiler then compiles frequently used methods. The program reaches full speed only after this.
Why a small program still loads many classes
A program that looks tiny can pull in a large part of the JDK. Streams, lambdas, and method references are good examples. A lambda causes the JVM to generate a class at run time, and the stream pipeline loads many helper classes.
You can see this yourself. The following commands compile the example from the topic and count the classes loaded during one run:
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.class
java -Xlog:class+load -cp app.jar Hello | grep -c "source:"Each class+load log line ends with a source: field. Most JDK classes show a shared objects file as their source. That is the JDK's default Class Data Sharing archive, which is already a startup optimization. To see the cost without it, turn it off:
java -Xshare:off -Xlog:class+load -cp app.jar Hello | grep -c "jrt:/"With sharing off, JDK classes come from the runtime image (jrt:/) and must be parsed and verified on every run. Your own classes and library classes never come from the default archive. They are always loaded from the JAR, parsed, and linked at startup.
Which costs are repeated work
Most of the time spent in phases 2 and 3 repeats identical work. The same JAR gives the same bytes. Parsing them produces the same result each time, and the same verification passes each time. The JVM throws all of it away when the process exits and does it again on the next launch.
That repeated work is what the AOT cache targets. The cache stores the result of loading and linking once, so later runs can reuse it.
| Phase | Same result every run? | Reduced by the AOT cache? |
|---|---|---|
| JVM initialization | Mostly | Little |
| Class loading and parsing | Yes, for the same classpath | Yes |
| Verification and linking | Yes, for the same classes | Yes |
| Static initializers and `main` | Depends on your code | Not as a rule |
| JIT warmup | No, depends on the run | Not in JDK 24. See the chapter on method profiling. |
A rough way to tell where your time goes
Run the application with -Xlog:class+load and look at the number of classes and the timestamps. Add -Xlog:startuptime style checks only after you know loading matters. A simple rule of thumb:
- Many hundreds or thousands of classes loaded from JARs, with startup in the seconds range, suggest that class loading and linking dominate.
- Few classes loaded and a long delay before the first output suggest the time is in your own code or in waiting on external systems.
Exercise
A service takes 4 seconds to start. A class+load log shows 9,000 classes loaded from application JARs. Name two startup phases the AOT cache can shorten for this service, and one phase it cannot.
Show answer
It can shorten class loading (parsing the class bytes) and linking (verification and resolution), because both give the same result on every run with the same classpath. It does not shorten work done in your own static initializers or main, such as opening connections. In JDK 24 it also does not remove JIT warmup.