Sub topic 7
How methods move between tiers
This chapter assumes you know what the five tiered compilation levels (0 to 4) are, and that HotSpot counts method invocations and loop back-edges to find hot code. Here we look at the rules that decide which level a method goes to next, and how to see those decisions in a running program.
The default path: 0, 3, 4
Most methods that become hot follow one path:
- The method starts at level 0, in the interpreter. The interpreter collects a few counters, and at higher load it may also collect profile data.
- When the counters cross the level 3 thresholds, C1 compiles the method at level 3. This code is fully optimized by C1 but carries profiling instructions.
- The profiled code keeps running and records branch outcomes, receiver types at call sites, and counts. When the level 4 thresholds are crossed, C2 compiles the method using that profile.
- Level 4 code has no profiling overhead. It stays in place until it is deoptimized or evicted.
The profile from step 3 is the reason level 3 exists. C2 needs to know which branches are rarely taken and which types appear at a call site, and level 3 is where that data is collected.
The thresholds
Each transition is a test on two counters: the invocation count i and the back-edge count b. HotSpot moves a method from level 0 to level 3 when either condition holds:
i > Tier3InvocationThreshold(default 200), ori > Tier3MinInvocationThreshold(default 100) andi + b > Tier3CompileThreshold(default 2000).
The move from level 3 to level 4 uses the same shape with larger defaults: Tier4InvocationThreshold is 5000, Tier4MinInvocationThreshold is 600, and Tier4CompileThreshold is 15000. Loop-heavy code is also checked against Tier3BackEdgeThreshold (60000) and Tier4BackEdgeThreshold (40000). A back-edge hit in a long-running loop triggers an on-stack replacement (OSR) compile instead of waiting for the next call.
The thresholds are not fixed in practice. HotSpot multiplies them by a scaling factor that grows with the length of the compile queue for each compiler thread. When C2 is behind, methods must be hotter before they are queued for it.
The other paths
The default path has several variations. Each exists for a reason.
| Path | When it happens | Why |
|---|---|---|
| 0 → 3 → 4 | Normal hot method | Profile with C1, then optimize with C2 |
| 0 → 2 → 3 → 4 | The C2 queue is long | Level 2 has only light counters, so it is cheaper than level 3. HotSpot compiles there as a stopgap and moves to level 3 once the queue drains. |
| 0 → 3 → 1 | The method turns out to be simple or C2 cannot or should not compile it | Level 1 is C1 code without profiling. C2 would gain nothing, so profiling is dropped. |
| 0 → 1 | Trivial methods such as simple getters | The method is cheap and C2 cannot improve it, so the profiled stage is skipped. |
| 4 → 0 | Deoptimization | The compiled code was invalidated. The method runs in the interpreter again and can climb the tiers a second time. |
Level 1 code is the final form for trivial methods. It is the only level where C1 code is meant to stay for good.
Observing the transitions
The following program has a trivial getter, a small hot method, and a hot loop in main.
public class TierPaths {
private final int value = 7;
int getValue() {
return value;
}
static int mix(int x) {
int h = x * 31;
h ^= (h >>> 7);
h += (h << 3);
return h;
}
public static void main(String[] args) {
TierPaths t = new TierPaths();
long sum = 0;
for (int i = 0; i < 20_000_000; i++) {
sum += mix(i) + t.getValue();
}
System.out.println(sum);
}
}Compile it and filter the compilation log down to its own methods:
javac TierPaths.java
java -XX:+PrintCompilation TierPaths | grep "TierPaths::"You should see lines like the following. The timestamps, compile IDs, and exact sequence vary from run to run.
85 41 1 TierPaths::getValue (5 bytes)
88 44 3 TierPaths::mix (21 bytes)
90 47 % 3 TierPaths::main @ 9 (48 bytes)
94 52 4 TierPaths::mix (21 bytes)
96 55 % 4 TierPaths::main @ 9 (48 bytes)The columns are the time in milliseconds since start, the compile ID, flags, the tier, and the method with its bytecode size. Read them as follows:
- A
%in the flags column marks an OSR compile.@ 9is the bytecode index of the loop that triggered it. getValueis compiled directly at level 1 because it is trivial.mixandmainfirst appear at level 3 and later at level 4. The level 4mainis an OSR compile too, because the loop never returns to give a normal call a chance to switch.- A line ending in
made not entrantmeans the earlier version of that method was retired. This happens when a method is replaced by a better tier, and also on deoptimization. The first case is routine. The second is covered in the deoptimization chapter.
You will often see the level 3 version of a method become "not entrant" right after the level 4 version is installed. The level 3 code is not wrong. It has simply been superseded.
Why a method can stay at level 3
A method may sit at level 3 for a long time. Common reasons:
- It never crosses the Tier4 thresholds. This is normal for code that runs only a few thousand times.
- Its C2 compile is waiting in the queue behind larger methods.
- C2 gave up on it. Very large methods, or methods with constructs C2 cannot handle, can fail to compile at level 4. In that case HotSpot may recompile the method at level 1 and continue without profiling.
Level 3 code is noticeably slower than level 1 or 2 code because every call and branch updates profile data. A hot method stuck at level 3 is a real cost, and the log is the way to see it.
Flags that change the path
You can force a particular path to confirm your understanding:
-XX:TieredStopAtLevel=1stops at C1 without profiling. Methods use 0 → 1 and never reach level 4.-XX:TieredStopAtLevel=3stops after the profiling tier. This is useful when you want to see the cost of profiled code on its own.-XX:-TieredCompilationremoves the intermediate levels. The interpreter does the profiling and C2 compiles from there.-XX:Tier3InvocationThreshold=Nand-XX:Tier4InvocationThreshold=Nchange the main thresholds. Lowering them makes compilation start sooner at the cost of compiling code that may not be hot.-XX:+PrintTieredEventsprints the decision events behind each transition. The output is verbose. Use it on small programs only.
Change one flag at a time and compare the PrintCompilation logs.
Common mistakes
- Assuming every method reaches level 4. Most methods in a typical application never do. Trivial ones stop at level 1, and cold ones stay in the interpreter or at level 3.
- Reading
made not entrantas a failure. It marks a version that is no longer used for new calls. Only the reason matters, and the log does not always state it. - Treating the thresholds as exact. The scaling for queue length means the point at which a method is compiled changes with load.
- Expecting the same log on every run. Compilation runs on background threads and depends on timing.
Exercise
In a PrintCompilation log, a method first shows tier 2, then tier 3, then tier 4. What most likely explains the tier 2 step, and why is level 2 used instead of going straight to level 3?
Show answer
The C2 compile queue was long when the method first became hot. Level 2 code only has light invocation and back-edge counters, so it is cheaper than level 3 and does not slow the method as much while C2 is busy. When the queue shortened, HotSpot recompiled at level 3 to collect the full profile that C2 needs, and then at level 4.
Exercise
A trivial getter appears in the log only at tier 1 and never at tier 4. Is this a problem?
Show answer
No. Trivial methods are compiled straight to level 1, C1 code without profiling, because C2 cannot optimize them further. Staying at level 1 is the intended final state.