The labs on this site need JavaScript to run. The same content is written out below.
Learn › Java › JVM
JIT warmup
Why are the first requests right after a deployment slow?
Why it exists — If bytecode is only interpreted one instruction at a time, the same file runs on any operating system but runs slowly, and if it is compiled to machine code from the start, it is fast but must be built separately for each operating system and CPU. The JIT compiler picks only the frequently used code while the program runs and turns it into machine code for that machine, so the same bytecode runs much faster than with interpretation alone.
Where it's used today — A typical JVM such as HotSpot always does this without being turned on, so developers mostly think about it right after a deployment and when measuring performance. Examples are calling the main APIs in advance before passing requests to a new server, and discarding the first stretch when measuring performance. For short-lived programs where startup speed matters, a method that compiles to machine code in advance, such as a GraalVM native image, is sometimes used.
Java code is compiled to bytecode, and the JVM runs it by interpreting it one bytecode instruction at a time. The JVM finds the methods that run often and compiles them again, this time to machine code (JIT), and from then on they run much faster.
Checked against real execution · Values actually measured on JDK 21
Concepts in this lab
- JIT compiler
- This JVM component compiles frequently executed bytecode to machine code while the program runs, and from then on runs that machine code directly. At first the interpreter interprets and runs the bytecode one instruction at a time; then the JVM finds the parts that run repeatedly and optimizes them. This is why a Java application is faster after it has run for a while than it is right after startup (warmup).
- Bytecode
- Where it's used today — If you view the bytecode of a .class file with javap -c, you can see how the compiler transformed the code. Spring and Hibernate create bytecode directly at run time to generate proxy classes.
- This is the intermediate form of code that the Java compiler (javac) produces from source code, and it is stored in .class files. It is not the machine code of a particular CPU or operating system but a set of instructions that the JVM understands, so it runs on the JVM of any platform.
- Java Virtual Machine (JVM)
- Where it's used today — Programs written in Kotlin and Scala, not only Java, are also compiled to bytecode and run on the JVM. On a server, you start a program by setting how it runs, such as the heap size (-Xmx) and the kind of GC, with JVM options.
- This is the program that reads and runs Java bytecode. Each operating system has its own version of it, so a Java program that is compiled once can run on several operating systems without modification. It loads classes, manages memory (garbage collection), and converts bytecode to machine code and runs it.
- Class loader
- Why it exists — If where and how to read classes were fixed inside the JVM, it could not handle varied locations such as a jar bundled in a jar file or a library folder for each web application. The class loader separates out the way of reading, so a new loader can be created and used when needed.
- Where it's used today — Tomcat gives each web application its own class loader so that different versions of the same library can run together, and Spring Boot uses a dedicated class loader that reads the library jars bundled in the executable jar.
- This JVM component finds class files and reads them into memory. Classes are not all read when the program starts; each class is loaded at the point when it is first needed. After a class is loaded, the JVM makes it ready for use through linking (verification, preparation, and resolution of symbolic references) and initialization (running static initializers).
Try these
- Default settings — The first batch takes 5.3ms, and the batches after it take about 1.4ms.
- With JIT turned off — It never gets faster. About 28ms per batch.
- With a warmup in advance — It is fast from the first user request.
Running with the default settings
- The Java compiler (javac) turns source code into bytecode, not machine code. When the program starts, the interpreter of the JVM interprets and runs this bytecode one instruction at a time. The bytecode runs on any CPU, but this is much slower than running machine code directly.
- Batch #1: 5.3ms. At first the code is run by the interpreter. The JVM counts how many times the method has been called and how many times the loop has run, and when the counts pass a threshold, it compiles this method to machine code. This batch ran as interpreted execution until the compiled code was ready (the compilation itself runs on another thread).
- Batch #2: 1.5ms. The code has started to run as compiled machine code. It is 3 to 4 times faster than the first batch. Using the information it gathered while running (such as which branches are taken often), the JVM optimizes the code once more, this time more heavily.
- Batch #3: 1.4ms.
- Batch #4: 1.4ms.
- Batch #5: 1.4ms.
- Batch #6: 1.4ms.
- Batch #7: 1.4ms.
- Batch #8: 1.4ms.
- Batch #9: 1.4ms.
- Batch #10: 1.4ms.
- Batch #11: 1.4ms.
- Batch #12: 1.4ms.
Result of this run
- The code is the same, but its speed at the start and later is different. The JVM does not compile all the code in advance. It picks only the parts that actually run often and compiles them while the program is running (just-in-time). Because it optimizes based on information from the actual execution, once the code is fully warmed up it runs at a speed close to that of a language compiled in advance.
- Right after a deployment, the code has not been JIT-compiled yet, and on top of that, classes are still being loaded, the connection pool is still being filled, and the caches are empty, so the first requests are especially slow. This is also why traffic is moved to a new server a little at a time and not all at once. When you measure performance, too, discard the first period and look at the values after the code is fully warmed up.
Code
public class JitWarmup { // a class made up for this example
static long work(int n) { // the method that was measured (a name made up for this example)
long s = 0;
for (int i = 0; i < n; i++) s += (i * 31L) ^ (s >>> 3);
return s;
}
public static void main(String[] args) {
long sink = 0; // uses the results so that the calculation is not removed
for (int batch = 1; batch <= 12; batch++) {
long t = System.nanoTime();
for (int k = 0; k < 2000; k++) sink += work(1000); // one batch = 2,000 calls
System.out.println("Batch " + batch + ": " + (System.nanoTime() - t) / 1000 + " microseconds");
}
System.out.println(sink);
}
}
// Run: java JitWarmup.java
Assumptions of this model
The numbers were measured by running the code above on JDK 21 in our test environment five times in each of the three ways, and the values shown are from the first run of each. In all five runs, with the default settings the first batch took 5.1 to 5.4ms and the batches after it 1.4 to 1.7ms; when warmed up in advance, the first batch took 1.4 to 1.9ms; and with -Xint each batch took 23 to 36ms. The numbers vary with the CPU and with when the code is run, but the shape is the same: the first period is slow, and the code gets faster after that. A JVM with the default settings applies a light compilation (C1) and a strongly optimizing compilation (C2) in stages.
Previous lab: GC and memory leaks · Next lab: Class loading and initialization