Sub topic 14
JIT Compilation and Warmup
This chapter assumes you can run Java from the command line, know what a heap and a thread are, and have read the earlier chapters on the JVM runtime model and profiling approaches. Its focus is why a Java program is slow at first and faster later, and what that means when you measure.
Why the first seconds are slow
The JVM starts by interpreting bytecode. That is quick to begin but slow to run. While interpreting, the JVM counts how often each method is called and how often each loop runs. When a count passes a threshold, the method is "hot" and the JIT (just-in-time) compiler turns it into machine code.
HotSpot uses tiered compilation:
- Tier 0 is the interpreter, which also collects profile data.
- Tiers 1 to 3 are the C1 compiler. It compiles quickly and produces moderately fast code.
- Tier 4 is the C2 compiler. It compiles slowly but uses the collected profile to produce highly optimized code, for example by inlining calls and removing unneeded checks.
C2 optimizes based on assumptions, such as "this call site only ever sees one class". If an assumption turns out wrong, the JVM discards the compiled code and falls back to the interpreter. This is called deoptimization. The method then warms up again.
What warmup means for measurement
Warmup is the period before the JIT has compiled the hot code. During it, throughput is lower and latency is higher. This causes common measurement errors:
- Timing a single run of a method with
System.nanoTime()measures mostly interpreter speed. - Comparing two implementations without warmup can favor whichever one the JIT happened to compile first.
- Profiling the first seconds of a service shows interpreter and compiler activity, not steady-state behavior.
You can watch the JIT work with -XX:+PrintCompilation:
java -XX:+PrintCompilation SlowEach line shows a timestamp, a compile ID, and the method. Repeated lines for the same method at different tiers show it moving up. A line containing "made not entrant" shows the old code being retired, often after deoptimization.
Seeing warmup in your own code
This program times the same work repeatedly. The early rounds are usually slower than the later ones.
public class Warmup {
static long work() {
long sum = 0;
for (int i = 0; i < 2_000_000; i++) {
sum += Integer.toString(i).length();
}
return sum;
}
public static void main(String[] args) {
long total = 0;
for (int round = 1; round <= 10; round++) {
long start = System.nanoTime();
total += work();
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println("round " + round + ": " + ms + " ms");
}
System.out.println(total);
}
}Printing total keeps the JIT from discarding the work. The exact times depend on your machine, but the first round is typically the slowest.
Practical consequences
- For a long-running service, measure and profile after it has handled steady traffic for a while. Many teams run a warmup load before taking traffic or before recording.
- Short-lived programs such as command-line tools may spend most of their life in the warmup phase. Optimizing them means reducing work at startup, not tuning hot loops.
- A latency spike shortly after a deployment may be warmup. Check whether it disappears after a few minutes.
- Recurring slowdowns of a method that was fast earlier can point to deoptimization.
-XX:+PrintCompilationshows it.
Exercise
You time a new sorting routine once, in a fresh JVM, and it looks slower than the old one. Why is this result unreliable, and what two changes make it more trustworthy?
Show answer
A single run in a fresh JVM mostly measures interpreted and partly compiled code, so the result depends on when the JIT compiled each routine. Run the code many times before measuring (warmup), then measure repeated iterations in steady state, ideally in several separate JVM runs. JMH does this for you.