Sub topic 1
Where JVM startup time goes
This chapter breaks JVM startup into its phases. It shows which phases repeat on every run and which of them the AOT cache can remove. Later chapters cover the cache itself.
Prerequisites
This chapter assumes you already know the following:
- You can compile and run a Java class or JAR from the command line.
- You know what the classpath is and what a JAR file contains.
- You have a rough idea that the JVM loads classes on demand and that the JIT compiler turns hot bytecode into machine code.
- You can run shell commands and time them with
time.
You need a JDK 17 or later to run the examples in this chapter. The AOT cache itself needs JDK 24 or later and starts in a later chapter.
What "startup time" means
Startup time is the time from launching the java command until the application does its first useful work. For a command-line tool, that is the time until the program finishes. For a server, it is the time until the first request can be answered.
Two things make Java startup feel slow compared with a native binary:
- The JVM is a program that must start before your program can. It initializes memory, threads, and its own runtime.
- Your bytecode is not machine code. Every class must be found, read, checked, and prepared before it runs, and the code runs slowly at first.
The second point accounts for most of the time that you can influence. It is also the part the AOT cache targets.
The phases of a JVM start
A typical start passes through these phases in order.
The next sections describe each phase and how much of it you can reduce.
1. JVM boot
The launcher finds the JVM library, parses options, reserves the heap address space, creates the garbage collector threads, and sets up internal tables. This work does not depend on your application. It takes a small, roughly constant amount of time, and your code cannot change it much. Flags such as the choice of garbage collector and the heap size affect it slightly.
2. Loading JDK core classes
Before main runs, the JVM needs classes like java.lang.Object, java.lang.String, and the module system classes. Since JDK 12, the JDK ships with a default Class Data Sharing (CDS) archive. This is a file that stores a pre-parsed form of about a thousand common JDK classes. The JVM maps it into memory instead of parsing each class file again. You get this benefit without any flag.
You can see the effect by turning the archive off with -Xshare:off. The same program then starts noticeably slower, because the JVM must load these classes from the runtime image the slow way.
3. Loading application classes
For every class your program uses for the first time, a class loader must do three things:
- Find the bytes: search the classpath, open a JAR, locate the entry, and decompress it.
- Parse the bytes into the JVM's internal representation of the class.
- Create the class object and record it in the loader's tables.
This happens lazily, when the code first needs a class. A program with a large dependency tree can need thousands of classes before it is ready. Each lookup in a JAR costs file system work and decompression, so classpath length matters too.
4. Verification and linking
After loading, a class is linked before it can be initialized. Linking has three steps:
- Verification checks that the bytecode is well formed and type-safe. This protects the JVM from corrupt or malicious class files. The check does real analysis of every method body.
- Preparation allocates memory for static fields and gives them default values.
- Resolution turns symbolic references, such as a method name in the constant pool, into direct references. This can be done lazily.
Classes from the JDK archive were already verified when the archive was built. Your application classes, and the libraries on your classpath, are verified on every run. The next chapter covers this phase in detail.
5. Static initializers and application setup
Once a class is linked, the JVM runs its static initializer the first time the class is used. After that, your own startup code runs. In frameworks, this is often the largest block of time. Examples are:
- scanning the classpath for annotated classes
- reading configuration files
- building dependency injection containers
- creating reflection metadata
- opening connection pools
The AOT cache does not remove this work. If your application spends two seconds building a container, the cache will not shorten that. It only removes the class loading and linking cost around it.
6. Interpretation and JIT warmup
The JVM starts every method in the bytecode interpreter. It counts how often methods run and loops iterate. When a method becomes hot, the JIT compiler translates it to machine code on background threads. Until then, the code runs slowly.
This is called warmup. The first request to a server is slower than the thousandth, because the code has not yet been compiled. Warmup is a separate cost from class loading, and the cache attacks it separately. A later chapter covers method profiling.
Lambdas and method references add hidden work
Code that looks small can trigger a lot of startup work. A lambda or a method reference is compiled to an invokedynamic instruction. The first time that instruction runs, the JVM calls a bootstrap method. That method generates a new hidden class for the lambda at run time. It also loads many java.lang.invoke classes to do so.
This is why the example below makes a good test subject. It is small, but it uses streams, a lambda-style method reference, and a collector.
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);
}
}The program prints one line, ADA, GRACE, LINUS. Yet it pulls in the stream pipeline classes, the collector classes, and the machinery to spin up a lambda class.
Seeing the cost yourself
You can measure how many classes the JVM loads, and where each one comes from. Build the program first:
javac Hello.java
jar --create --file app.jar --main-class Hello Hello.classNow run it with class loading logging written to a file. The class+load log tag prints one line per loaded class, including its source.
java -Xlog:class+load:file=load.txt -cp app.jar Hello
# Total classes loaded
wc -l < load.txt
# Classes that came from the default CDS archive
grep -c "source: shared objects file" load.txt
# Classes that did not (loaded from the runtime image, a JAR, or generated)
grep -vc "source: shared objects file" load.txtYou should see several hundred lines in total. Most of them come from the shared archive. The rest show where the work remains. Look at what is not shared:
grep -v "source: shared objects file" load.txt | head -n 20Expect to see Hello itself, loaded from app.jar, and some generated lambda classes whose source is not a file. Classes from the JDK that the default archive does not hold appear with a jrt:/ source. The exact count varies by JDK version and platform, so compare your own two runs and not published numbers.
Now compare against a run with the default archive disabled:
java -Xshare:off -Xlog:class+load:file=load-off.txt -cp app.jar Hello
grep -c "source: shared objects file" load-off.txtThe count of shared classes drops to zero. Time both runs:
time java -cp app.jar Hello
time java -Xshare:off -cp app.jar HelloOn most machines the second command is clearly slower. This difference is the benefit of the default CDS archive. The AOT cache extends the same idea to your classes.
Which costs the AOT cache removes
The table summarizes how each phase relates to the AOT cache covered in this tutorial.
| Phase | Repeats on every run? | Removed by the AOT cache? |
|---|---|---|
| JVM boot | Yes | No |
| Loading JDK core classes | Yes, but the default CDS archive already helps | Largely covered already |
| Loading application classes | Yes | Yes, for classes seen in the training run |
| Verification and linking | Yes | Yes, done once when the cache is built |
| Static initializers and setup | Yes | No, with limited exceptions |
| Interpretation and JIT warmup | Yes | Partly, through cached method profiles in JDK 25 |
The pattern is that the cache moves repeatable work from every run to a single build step. Work that depends on the environment or on input, such as reading configuration or opening connections, cannot move, because its result differs from run to run.
A way to decide whether class loading matters for you
A quick check helps you decide whether the cache is worth the effort for a given application:
- Count the loaded classes with the
class+loadlog, as shown above. A few hundred classes means little to gain. Many thousands means more. - Count how many come from JARs on your classpath, which are the ones the default archive does not cover. Search for
source: file:lines. - Compare total startup time against
-Xshare:offto get a feel for how much class loading costs on your hardware. - Take a thread dump or a profile during startup. If most samples are in
ClassLoader,URLClassPath, or verification, the cache will help. If they are in your own code, it will not.
grep -c "source: file:" load.txtFor Hello, this prints a small number, since only Hello comes from the JAR. In a framework application, the same command prints a large number. That number measures the work the AOT cache can remove.
Common mistakes when reasoning about startup
- Treating startup as one number. Boot, class loading, application setup, and warmup have different causes and different fixes. Measure them separately.
- Confusing startup with peak speed. The cache improves how quickly the program gets going. It does not make steady-state code faster.
- Forgetting that
-Xshare:offis a diagnostic. Use it to understand costs. Do not ship it. - Measuring with a different classpath. Class loading costs depend on the JARs present. Measure with the same classpath that you deploy.
Exercise
You run your application with -Xlog:class+load and find that 4,200 classes were loaded. 600 came from the shared objects file, and 3,600 came from JARs on the classpath. Which phases of startup does this suggest are worth attacking with a cache, and which would a cache not help?
Show answer
The 3,600 classes loaded from JARs go through finding, parsing, verification, and linking on every run, because the default CDS archive does not cover them. These are the phases a cache can remove. A cache would not help with the JVM boot, with static initializers and application setup code such as classpath scanning or configuration reading, or with the part of warmup that depends on JIT compilation (apart from the cached profiles in JDK 25). To confirm that class loading dominates, compare total startup time against the time spent in your own setup code.
Exercise
Why does a short program that uses String::toUpperCase in a stream load many more classes than its source code suggests?
Show answer
A method reference compiles to an invokedynamic instruction. On first execution, the JVM runs a bootstrap method that loads many java.lang.invoke classes and generates a hidden class for the lambda at run time. The stream pipeline and the collector also bring in their own implementation classes. All of this happens during the run, so each run pays for it again unless a cache stores the result.