Sub topic 6
Tiered Compilation: Levels 0 to 4
Assume you know Java methods, loops, interfaces, and the difference between Java bytecode and native machine code. You should also be comfortable running java from a command line.
HotSpot does not send every method directly to its most advanced compiler. Instead, a method usually starts in the interpreter and moves through compilation levels as HotSpot gathers evidence about how often and how consistently the method runs. This staged process is called tiered compilation.
The five levels
| Level | Component | Main purpose |
|---|---|---|
| 0 | Bytecode interpreter | Run code immediately and collect basic execution information |
| 1 | C1 compiler | Produce simple native code with little or no profiling |
| 2 | C1 compiler | Produce native code with limited profiling |
| 3 | C1 compiler | Produce native code with detailed profiling |
| 4 | C2 compiler | Apply expensive, highly optimizing compilation |
The interpreter is level 0. It needs no compilation time, so startup is fast. However, interpreting every instruction is usually slower than running compiled native code.
C1 is the client compiler. It compiles quickly and produces useful native code. Its higher profiling levels collect information such as which branches are common and which concrete classes appear at virtual calls.
C2 is the server compiler. It takes longer to compile, but it uses more profiling data and performs more aggressive optimizations. A hot method often reaches level 4 after first running at a C1 level.
Common paths through the levels
With tiered compilation enabled, a typical method follows a path like this:
The exact path depends on invocation counts, loop back-edge counts, compiler queue pressure, and the profiling information already available. A short-lived method may never leave level 0. A frequently called method may move from level 0 to level 3 and later to level 4.
A method can also be compiled while another thread continues running the old version. Compiler threads work in the background. When a compiled version is ready, HotSpot redirects future calls to it.
Seeing the levels
Run a program with compilation logging enabled:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopThe output contains method names and compilation levels. A level such as 3 usually indicates C1 compilation with full profiling. A level such as 4 indicates C2 compilation. Output can vary between runs because thresholds and compiler queues depend on the machine and workload.
Why the staged approach helps
Tiered compilation balances three competing costs:
- Interpretation gives fast startup but slower steady-state execution.
- C1 gives useful native code quickly and gathers profiling data.
- C2 spends more time compiling only methods likely to repay that cost.
This design is especially useful for server programs. Cold startup code remains cheap to run, while repeatedly executed request handlers can eventually receive aggressive optimization.
Exercise
A method is called only once during a short command-line program. Which level will most likely execute it, and why?
Show answer
Level 0, the interpreter, will most likely execute it. The method may finish before its invocation or loop counters reach a compilation threshold.