Java JIT Tiered Compilation With C1 and C2 Explained
Understand JIT tiered compilation: the interpreter, C1 and C2 tiers, profiling, inlining, deoptimization, warm up and the flags that matter.

The JVM does not compile your program ahead of time. It watches the program run and compiles what turns out to be worth compiling. That is why the first requests after a deploy are slower than the thousandth, why a microbenchmark can lie to you by a factor of thirty, and why a service seems to speed up for the first minute and then stay put. The bytecode foundation is in how the JVM works; this post is about what the JVM does with the hot parts.
All the numbers below come from small programs run on JDK 17.0.1 on a 10-core ARM laptop. Absolute times will differ on your hardware, but the ratios and the shape of the curve are what matter. The mechanism is the same in Java 25.
The same loop at three speeds
This program runs a tiny method twenty million times, six rounds in a row, and prints how long each round took:
public class Warm {
static int work(int x) {
return (x * 31) ^ (x >>> 3);
}
public static void main(String[] args) {
long sum = 0;
for (int round = 1; round <= 6; round++) {
long t0 = System.nanoTime();
for (int i = 0; i < 20_000_000; i++) {
sum += work(i);
}
System.out.printf("round %d: %d ms%n", round, (System.nanoTime() - t0) / 1_000_000);
}
System.out.println(sum);
}
}Run it three ways and compare the first rounds:
java Warm # default: tiered compilation
java -Xint Warm # interpreter only, no JIT
java -XX:TieredStopAtLevel=1 Warm # C1 only, no C2default: round 1: 15 ms round 2: 17 ms round 3: 13 ms ... round 6: 13 ms
-Xint: round 1: 467 ms round 2: 465 ms round 3: 468 ms
C1 only: round 1: 16 ms round 2: 15 ms round 3: 15 msThe interpreter takes about 465 ms per round. Compiled code takes 13 to 17 ms. That is a factor of thirty from the compiler alone, and it is the reason HotSpot has one. C1 alone reaches 15 ms here; C2 shaves it to 13 ms on this loop. The gap between C1 and C2 is small on a trivial method and large on code with calls, virtual dispatch and allocation, where C2's inlining and escape analysis do real work. Notice also that round 1 on the default run is already fast. The JIT compiled the loop within the first few milliseconds, so the warm-up hid inside round 1 of a 20-million-iteration loop. A short-lived request handler does not get that luxury.
The tiers
HotSpot has five levels, numbered 0 to 4, and a method climbs them as counters fill:
| Tier | Executed by | Collects a profile? | Used for |
|---|---|---|---|
| 0 | Interpreter | Yes, a little | Every method on its first calls |
| 1 | C1, no profiling | No | Trivial methods C2 would not improve |
| 2 | C1, light profiling | Invocation and loop counts | Only when C2's queue is full |
| 3 | C1, full profiling | Types, branches, counts | The normal stop on the way up |
| 4 | C2, fully optimized | No | Hot methods, with a profile to guide the optimizer |
The usual path is 0, 3, 4. A method begins in the interpreter. Once its invocation count or the back-edge count of a loop crosses a threshold, it is queued for C1 at tier 3, which compiles quickly but inserts counters that record which types reached each call site and which way each branch went. When the counters get large, the same method is queued for C2, which uses that recorded profile to optimize for the behavior it actually saw.
You can watch the climb with -XX:+PrintCompilation. The columns are the time in milliseconds, a compile id, flags, the tier and the method:
java -XX:+PrintCompilation Warm | grep 'Warm::' 18 4 3 Warm::work (9 bytes)
18 5 4 Warm::work (9 bytes)
18 4 3 Warm::work (9 bytes) made not entrant
19 6 % 3 Warm::main @ 18 (90 bytes)
19 7 3 Warm::main (90 bytes)
20 8 % 4 Warm::main @ 18 (90 bytes)
21 6 % 3 Warm::main @ 18 (90 bytes) made not entrantwork is compiled at tier 3 at 18 ms, then at tier 4 in the same millisecond, and the tier 3 version is marked "made not entrant" so no new calls enter it. The % marks an on-stack replacement (OSR): main is stuck inside one long loop, so the JVM compiles the loop body at bytecode index 18 and jumps into the new code mid-loop without waiting for the method to return. OSR is why a single main with a giant loop benefits from the JIT at all.
What C2 does with a profile
The profile is not decoration. C2 makes bets on it:
- Inlining. A call site that always saw one concrete type is inlined as if it were a direct call. The method body is pasted in, which removes the call and exposes the body to further optimization.
- Monomorphic and bimorphic dispatch. An interface call that only ever reached one or two implementations becomes a type check plus a direct jump instead of a vtable lookup.
- Escape analysis. An object that never leaves the method can live in registers, or be dissolved into its fields, so
newcosts nothing and the garbage collector never sees it. - Branch layout. A branch taken 99.9 percent of the time is laid out as the fall-through, and the cold side is moved away.
- Loop optimizations. Unrolling, range check elimination and vectorization turn simple array loops into a few SIMD instructions.
The practical result is that tiny abstractions are free after warm-up. Small getters, lambdas and builders inline away, which is why you should write clear code and let the JIT flatten it. The opposite also holds: a call site that sees ten different implementations is megamorphic and no longer inlined, and a code pattern that looks identical to the one above can run several times slower.
Deoptimization: when a bet loses
C2 compiles for what it saw. If a later execution breaks the assumption, the JVM discards the compiled code and falls back to the interpreter, a step called deoptimization. Typical triggers:
- A new class is loaded that implements an interface at a call site that was inlined for one implementation.
- A branch that was never taken suddenly is.
- A type check that always passed starts to fail.
- A null that was never seen shows up.
The output shows it as "made not entrant" followed by a recompile. In the lab run above, the tier 4 version of the loop in main was invalidated at 34 ms and rebuilt: the first compiled loop had been specialized to a state the program soon left. Deoptimization is correct and cheap in small doses, but a service with a steady stream of it in -XX:+PrintCompilation after warm-up is paying for it in latency.
Warm-up in production
Because the profile is built from real traffic, the first minute of a JVM is the slowest. The pattern after a deploy is familiar: p99 latency is bad for the first hundred requests to each endpoint, then settles. Four things move the curve:
- Traffic shape. A profile taken from health checks alone optimizes the wrong code. Warm a new instance with representative requests before it enters the load balancer.
- CPU. The compiler threads compete with your request threads. On a pod limited to a fraction of a core, warm-up is much slower, see CPU limits in containers.
- Code cache. Compiled code lives in the code cache, 240 MB by default with tiered compilation. A full cache stops compilation and the log says
CodeCache is full. - Ahead-of-time help. Class Data Sharing and the AOT cache in recent JDKs shorten startup, and a native image skips the JIT entirely at the cost of peak throughput. See Micronaut native images for that trade.
Flags worth knowing
| Flag | Effect | When to touch it |
|---|---|---|
-XX:+PrintCompilation | One line per compilation | Diagnosis, never in production by default |
-XX:TieredStopAtLevel=1 | C1 only | Short-lived tools and tests where warm-up never pays back |
-XX:-TieredCompilation | C2 only, with a longer interpreter phase | Rarely; slower startup |
-XX:ReservedCodeCacheSize=512m | Larger code cache | When the log reports a full code cache |
-XX:CICompilerCount=n | Number of compiler threads | Constrained containers, after measuring |
-Xint | Interpreter only | Demonstrations, and ruling the JIT in or out |
Do not tune thresholds such as -XX:Tier4InvocationThreshold as a first move. The defaults are the result of years of benchmarking on varied code. If a flag is the answer, you will have a measurement that says so.
Benchmarks that survive the JIT
The biggest practical consequence is how you measure. A loop timed once, the way the lab program did in round 1, includes interpreter time, C1 time and the compile pauses. A fair test runs the code until the compiler has settled, discards that period, then measures. The tool for it is JMH, which handles the warm-up, forks a fresh JVM per trial and defends against the optimizer deleting dead code. Hand-rolled System.nanoTime() loops around a method with no visible result are the classic way to measure nothing: C2 removes the work and reports a flattering zero.
Where this does not apply
If your process lives for a few seconds, such as a CLI or a short batch step, the JIT never reaches C2 and -XX:TieredStopAtLevel=1 is often the right setting. If startup time is the metric that matters and peak throughput is not, ahead-of-time compilation changes the question, and the memory side of the same process is in JVM memory areas. The flag reference is in the HotSpot performance documentation.


