Sub topic 7
How Methods Move Between Tiers
This chapter assumes you know basic Java classes, methods, loops, and interfaces. You should also know that Java source is compiled into bytecode and that the java command runs that bytecode on the JVM.
HotSpot does not compile every method immediately. It begins by interpreting bytecode, then promotes methods that execute often. This lets the JVM start quickly while still optimizing the parts of the program that matter most.
The five compilation levels
HotSpot’s tiered compiler uses levels from 0 through 4:
| Level | Execution mode | Purpose |
|---|---|---|
| 0 | Interpreter | Start quickly and collect basic execution information |
| 1 | C1 compiler, simple compilation | Produce native code quickly with little profiling |
| 2 | C1 compiler, limited profiling | Add some profiling with modest compilation cost |
| 3 | C1 compiler, full profiling | Produce native code and collect detailed profile data |
| 4 | C2 compiler | Apply expensive, aggressive optimizations |
C1 is the fast compiler. C2 is the optimizing compiler. A method may first reach level 3 so HotSpot can gather useful profile data. If the method stays hot, C2 recompiles it at level 4.
The usual tiered path is:
Common transition paths
A method does not always visit every level. HotSpot chooses a path based on how often the method runs, how much compilation information is available, and how quickly compilation is needed.
Common paths include:
0 → 3 → 4: The normal tiered path for a method that becomes hot.0 → 2 → 3 → 4: The method receives limited profiling before full profiling.0 → 1: The method needs native code quickly but is not expected to benefit from detailed profiling.0 → 4: A path associated with non-tiered compilation, where C2 handles compilation directly.
The transition happens asynchronously. The running application continues while a compiler thread processes a compilation request. This prevents compilation from blocking the application for every method.
What triggers promotion
HotSpot maintains counters related to method invocations and loop back edges. A back edge is a jump from the end of a loop back to its condition or beginning. These counters approximate how much work a method performs.
A method with many calls can become hot even if it contains no large loop. A method with one long-running loop can become hot because its back-edge counter rises rapidly. When a threshold is reached, HotSpot places a compilation request on a compiler queue.
Thresholds are adaptive. They depend on the compilation level, available compiler resources, and JVM settings. Therefore, a method may compile at a different iteration count on another machine or run.
Observe the path
Compile this example and ask HotSpot to print compilation events:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopOutput varies by Java 11 build, but entries often include a compilation ID, a method name, and a level such as % or a level number. A % commonly indicates on-stack replacement for a loop. The level helps show whether C1 or C2 compiled the method.
For repeatable performance measurements, allow the program to warm up before recording timings. A first run includes interpretation, profiling, compilation, and possible replacement of earlier compiled code.
Exercise
Why is the path 0 → 3 → 4 common for a frequently executed method?
Show answer
Level 0 starts the method quickly, level 3 uses C1 to collect detailed profiling while running native code, and level 4 lets C2 use that profile for more aggressive optimization.