Sub topic 6
Tiered compilation: levels 0 to 4
This chapter assumes you know that HotSpot starts in the bytecode interpreter and that it has two JIT compilers, C1 and C2. It also assumes you know that invocation and back-edge counters detect hot code. Here you will see how those pieces combine into the five-level scheme called tiered compilation.
Why one compiler was not enough
C1 compiles quickly and produces decent code. C2 compiles slowly and produces excellent code, because it relies on detailed profile data. Before tiered compilation, you had to pick one:
- The client VM used C1 only. It started fast but peaked lower.
- The server VM used C2 only. It peaked high but ran slowly while the interpreter gathered profiles.
Tiered compilation uses both. A method starts in the interpreter and gets cheap C1 code quickly. It gets C2 code later, once enough profile data exists. In JDK 11 on 64-bit server VMs, tiered compilation is on by default.
The five levels
HotSpot numbers its execution modes 0 to 4.
| Level | Executor | Profiling | Typical role |
|---|---|---|---|
| 0 | Interpreter | Counters, plus type and branch profiles when needed | Where every method starts |
| 1 | C1, full optimization | None | Trivial methods that C2 cannot improve |
| 2 | C1, limited | Invocation and back-edge counters only | Temporary stop when the C2 queue is long |
| 3 | C1, full profiling | Counters, branch profiles, and type profiles | Default first compiled level |
| 4 | C2 | None. It uses the profile gathered earlier. | Peak-performance code |
Some details about each level:
- Level 0. The interpreter is slow but needs no compile time. It increments counters on method entry and loop back-edges.
- Level 1. This is C1 code with no profiling overhead. HotSpot picks it for methods it judges trivial, such as simple getters. C2 would produce about the same code, so there is nothing to gain by profiling.
- Level 2. This is C1 code that keeps only the counters. It is faster than level 3 because it skips the profile updates. It is used when the C2 queue is backed up (see below).
- Level 3. This is C1 code instrumented to record receiver types at call sites, taken and not-taken branch counts, and similar data. The instrumentation costs speed. HotSpot's design notes put level 3 code at roughly 30% slower than level 2.
- Level 4. C2 reads the profile and compiles with aggressive, speculative optimizations. These include inlining based on observed types, which a later chapter covers in detail.
How methods move between levels
The most common path in a running program is 0 → 3 → 4. A method is interpreted until its counters cross the level 3 thresholds. C1 then compiles it with full profiling. Level 3 code keeps collecting a profile until the level 4 thresholds are crossed. C2 then compiles it.
The diagram leaves out some rare transitions, but these paths cover what you will normally see. Levels 1 and 4 are final states. Nothing needs to recompile level 1 or level 4 code unless an assumption breaks. When that happens, HotSpot discards the compiled code and the method returns to level 0. This is deoptimization, covered in a later chapter.
What triggers each step
HotSpot compares counters against thresholds. Each threshold is a flag, and the defaults below are the JDK 11 values for a 64-bit server VM.
| Flag | Default | Meaning |
|---|---|---|
| `Tier3InvocationThreshold` | 200 | Invocations that trigger a level 3 compile |
| `Tier3MinInvocationThreshold` | 100 | Minimum invocations when combined with loop count |
| `Tier3CompileThreshold` | 2000 | Invocations plus back-edges, if invocations pass the minimum |
| `Tier3BackEdgeThreshold` | 60000 | Back-edges that trigger an on-stack replacement compile |
| `Tier4InvocationThreshold` | 5000 | Invocations that trigger a level 4 compile |
| `Tier4MinInvocationThreshold` | 600 | Minimum invocations when combined with loop count |
| `Tier4CompileThreshold` | 15000 | Invocations plus back-edges |
| `Tier4BackEdgeThreshold` | 40000 | Back-edges that trigger a level 4 OSR compile |
A method is compiled if either test passes:
- its invocation count exceeds the invocation threshold, or
- its invocation count exceeds the minimum and invocations plus back-edges exceed the compile threshold.
The second test lets a method that is called a moderate number of times but contains a loop qualify sooner.
Load-sensitive thresholds
HotSpot adjusts these thresholds to the current load. The two controls are:
- If the C2 queue is long, the effective level 4 thresholds are scaled up, so fewer methods are submitted to C2.
-XX:CompileThresholdScaling=<factor>multiplies all thresholds. A value of 0.5 makes compilation happen twice as early. A value of 2.0 makes it half as early.
This is why level 2 exists. If many methods are waiting for C2, sending a method to level 3 would just add slow instrumented code while it waits. HotSpot compiles it at level 2 first, which runs faster, and moves it to level 3 once the queue shrinks.
Seeing the levels
Reuse the HotLoop program from the spine. The square method is tiny and called 50 million times, and main contains a long loop.
public class HotLoop {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
long sum = 0;
for (int i = 0; i < 50_000_000; i++) {
sum += square(i % 1000);
}
System.out.println(sum);
}
}Run it with different level limits:
javac HotLoop.java
# Default tiered behavior, showing each compile with its level
java -XX:+PrintCompilation HotLoop
# Stop at level 1: C1 only, no profiling, no C2
java -XX:TieredStopAtLevel=1 -XX:+PrintCompilation HotLoop
# Stop at level 3: C1 with full profiling, never reach C2
java -XX:TieredStopAtLevel=3 -XX:+PrintCompilation HotLoop
# Disable tiering: interpreter profiles, then C2 only
java -XX:-TieredCompilation -XX:+PrintCompilation HotLoop
# List every Tier flag and its current value
java -XX:+PrintFlagsFinal -version | grep TierIn -XX:+PrintCompilation output, the number between the sequence number and the method name is the level. A line such as HotLoop::square (6 bytes) with level 3 and later one with level 4 shows the normal 3 → 4 path. Exact timestamps and sequence numbers vary from run to run, so look at the pattern and not the numbers. A % in the flags column marks an on-stack replacement compile. That is how main's loop gets compiled while main is still running.
Compare total run times across the four commands. TieredStopAtLevel=1 often finishes short programs fastest, because it skips the profiling and C2 work. For longer runs the default usually wins, because level 4 code is faster.
Common errors and misconceptions
- Assuming level 1 is the "best C1" for every method. Level 1 is only for methods HotSpot judges trivial. Most hot methods spend time at level 3 first.
- Expecting level 3 to be fast. It carries profiling cost. A method stuck at level 3, for example because C2 compilation is slow or backed up, can be slower than it would be at level 2 or 4.
- Using
-XX:-TieredCompilationto "turn off the JIT". It does not. It leaves C2 as the only compiler. To turn off compilation entirely, use-Xint, which is far slower. - Treating the numbers as stable. Thresholds and the tier-selection policy can change between JDK versions. Check with
-XX:+PrintFlagsFinalon the JVM you actually run.
Summary of the model
Every method starts at level 0. HotSpot counts invocations and loop back-edges. Past the thresholds, C1 compiles the method, normally at level 3, which collects a profile. When the method stays hot, C2 uses that profile to produce level 4 code. Levels 1 and 2 are side routes for trivial methods and for a busy C2 queue. If a speculative assumption in compiled code fails, the method returns to level 0 and the cycle starts again.
Exercise
A method is called 150 times and contains a loop that runs 5,000 times on each call. With the default thresholds, can it be compiled at level 3, and why? Which test applies?
Show answer
Yes. Its invocation count (150) is below Tier3InvocationThreshold (200) but above Tier3MinInvocationThreshold (100). The second test then applies: invocations plus back-edges must exceed Tier3CompileThreshold (2000). The loop contributes 150 × 5,000 = 750,000 back-edges, far above 2000, so the method qualifies for level 3.
Exercise
You run java -XX:TieredStopAtLevel=1 HotLoop and then java HotLoop. For a program that runs for several minutes, which would you expect to reach higher steady-state speed, and why?
Show answer
The default run, because it reaches level 4. C2 uses the profile collected at level 3 to apply speculative optimizations. With TieredStopAtLevel=1, code is limited to level 1 C1 output and never gets those optimizations.
Exercise
Why does HotSpot sometimes compile a hot method at level 2 and not level 3?
Show answer
Level 3 adds profiling overhead and is meant to be temporary until C2 compiles the method. If the C2 queue is long, the method would stay at level 3 for a while. HotSpot compiles it at level 2, which only keeps counters and runs faster, then moves it to level 3 when the queue shortens.