ArchitectMind
  1. Home
  2. Java Ecosystem
  3. Book 2 · JVM, Concurrency & Performance
  4. Volume III
  5. Tiered Compilation
Book 2 · Volume III — JIT Compilation, HotSpot & JVM Performance · Topic 1.4

Tiered Compilation

Tiers, C1, C2, profiling and optimisation

11 min readStage 1 · Foundations11 interview questions
Prerequisites
1.3 HotSpot
Covers
Tiers · C1 · C2 · Profiling · Optimization
Related
Chapter 2 (C2 optimisations) · 3.3 Recompilation · 4.1 Startup Performance
On this page
  1. 1. Concept / Definition
  2. 2. Why It Exists
  3. 3. Core Concepts
  4. 4. How It Works Internally
  5. 5. APIs / Features / Syntax
  6. 6. Practical Examples
  7. 7. Common Problems & Mistakes
  8. 8. Performance & Production Considerations
  9. 9. Architecture & Design Considerations
  10. 10. Interview Questions & Answers

1Concept / Definition

Tiered compilation (default since Java 8) combines HotSpot's two JIT compilers into a pipeline of five levels. Code starts in the interpreter (level 0), moves to C1-compiled code that also collects profiles (level 3), and — if it stays hot — to highly optimised C2 code (level 4). Levels 1 and 2 handle trivial methods and busy-compiler situations. The goal is to get the fast startup of a quick compiler and the peak performance of an optimising compiler, with profiles collected by fast compiled code rather than by the slow interpreter.

Tiered compilation state machine
Figure 1 — Tiered compilation state machine

2Why It Exists

Before tiered compilation, users chose between the *client* VM (C1: fast warm-up, lower peak) and the *server* VM (C2: slow warm-up, high peak). Tiered compilation removes that choice: C1 quickly replaces interpreted code, profiling happens in compiled code where it costs less, and C2 invests its expensive optimisation only in code that has proven hot, using a rich profile.

Throughput over warm-up
Figure 2 — Throughput over warm-up

3Core Concepts

Tiers

LevelExecuted byProfilingWhen used
0interpretercounters (full profile only if C1 lags)every method at start
1C1nonetrivial methods (e.g. getters); methods C2 cannot compile — final state
2C1invocation + back-edge counters onlywhen the C2 queue is long; moved to 3 later
3C1full (branches, types, calls)normal path for warm methods
4C2nonehot, profiled methods — final state

Typical paths

  • Common: 0 → 3 → 4.
  • Trivial method: 0 → 1 (profiling would be wasted; C2 would not do better).
  • C2 busy: 0 → 2 → 3 → 4 (level 2 is faster than level 3 while waiting).
  • C2 fails or not beneficial: 3 → 1.
  • Deoptimisation: 4 → 0 (and then back up with an updated profile).

OSR at each tier

Back-edge thresholds exist for levels 3 and 4, so long loops can be OSR-compiled first by C1 and later by C2 (% entries in PrintCompilation at both levels).

Reading PrintCompilation output
Figure 3 — Reading PrintCompilation output

C1

  • Also called the *client compiler*; compiles quickly (typically milliseconds per method).
  • Builds HIR (a high-level SSA IR over basic blocks), performs value numbering, constant folding, simple inlining of statically bound methods (and CHA-devirtualised ones) and null-check elimination; lowers to LIR and uses linear-scan register allocation.
  • Level 3 code inserts profiling instrumentation into branches, calls and type checks — making it roughly 30% slower than level 2 code, but still far faster than the interpreter.
  • C1 inlining limits: C1MaxInlineSize (35 bytes), C1MaxInlineLevel (9).

C2

  • Also called the *server* or *opto* compiler; compiles slowly (tens to hundreds of milliseconds, more for large methods) and produces the fastest code.
  • Uses a sea-of-nodes SSA graph combining data and control dependencies; performs global value numbering, profile-guided inlining, escape analysis, loop transformations, vectorisation, intrinsics, and speculative optimisations guarded by uncommon traps.
  • Uses graph-colouring register allocation and instruction scheduling.
  • Responsible for most CPU time spent in a warmed-up service — Chapter 2 is about its optimisations.
C1 versus C2
Figure 4 — C1 versus C2
How the two compilers represent code
Figure 5 — How the two compilers represent code

Profiling

Why profile in C1 code

Profiling needs many executions to be statistically useful. Collecting it in C1 code — which runs perhaps an order of magnitude faster than the interpreter — gathers the same data in much less wall-clock time and with less CPU. Once a method reaches the tier-4 thresholds, its profile is rich enough for C2's speculative decisions.

Profile quality

  • Profiles reflect what ran during levels 0–3, not all future behaviour.
  • Workload shifts after C2 compilation (new receiver types, new branches) cause deoptimisation and re-profiling (Chapter 3).
  • The JDK 25 AOT cache (JEP 515) can store method profiles from a training run so C2 can compile earlier in production.

Optimization

Balancing cost and benefit

ConcernTiered policy response
StartupC1 code replaces interpretation quickly
Peak performanceC2 compiles hot methods with full profiles
Compiler overloadthresholds scale with queue length; level 2 when C2 is busy
Wasted efforttrivial methods stop at level 1; cold methods never compiled
Changing behaviourdeoptimisation to level 0, re-profiling, recompilation

Configuration options

Tiered-compilation settings
Figure 6 — Tiered-compilation settings

4How It Works Internally

The CompilationPolicy evaluates predicates using invocation and back-edge counts, the current level and queue lengths, whenever counters trigger notifications from interpreter or C1 code. Level-3 code increments counters in the MethodData and calls into the policy periodically. Compiler threads are split between C1 and C2 (roughly one third C1 and two thirds C2 by default, CICompilerCount ergonomic from CPU count). The code cache stores level 2–3 code in the *profiled* segment (short-lived, replaced by C2) and level 1/4 code in the *non-profiled* segment (long-lived), reducing fragmentation.

5APIs / Features / Syntax

SHELLTiered compilation settings
# Default tiered behaviour and the compiler thread split
$ java -XX:+PrintFlagsFinal -version | grep -E " TieredCompilation | TieredStopAtLevel | CICompilerCount "
     bool TieredCompilation   = true
     intx TieredStopAtLevel   = 4
     intx CICompilerCount     = 12          # e.g. on a 16-CPU machine

# Experiments (measure before adopting)
# C1 only: faster warm-up, lower peak
$ java -XX:TieredStopAtLevel=1 -jar cli-tool.jar
# C2 only: slow warm-up (for comparison)
$ java -XX:-TieredCompilation -jar service.jar
# fewer compiler threads in small containers
$ java -XX:CICompilerCount=4 -jar service.jar
SHELLAOT cache with method profiles
# JDK 25: train once, reuse loaded classes AND method profiles (JEPs 514, 515)
# training run with representative load
$ java -XX:AOTCacheOutput=app.aot -jar app.jar
$ java -XX:AOTCache=app.aot -jar app.jar                # production run
#   → earlier C2 compilation, shorter warm-up

6Practical Examples

WorkloadSettingRationale
Long-running API servicedefaults (tiered, level 4)best peak performance
CLI tool / short batch (< 10 s)TieredStopAtLevel=1 (measure)C2 never pays off; less compile CPU
Frequently autoscaled serviceAOT cache trained on real trafficshorter warm-up without losing peak
1-vCPU containerdefaults, maybe fewer compiler threadscompilation competes with app for CPU
BenchmarkingJMH defaults, long enough warm-upreach level 4 before measuring

7Common Problems & Mistakes

  • Disabling tiered compilation based on outdated advice.
  • Using TieredStopAtLevel=1 for long-running services to 'save CPU' — permanently gives up C2 optimisations.
  • Reading level-3 code performance as steady state in benchmarks.
  • Starving compiler threads with tight CPU limits, delaying C2 for minutes.

8Performance & Production Considerations

  • Track time until hot methods reach level 4 (JFR compilation events) as part of warm-up analysis.
  • In CPU-limited containers, compiler threads can consume a large share of CPU during warm-up — size requests/limits accordingly.
  • Profiled-code heap pressure appears when many methods sit at level 3 (e.g. C2 starved) — watch code-cache segment usage.

9Architecture & Design Considerations

Tiered compilation is why modern Java offers good startup *and* excellent peak performance without configuration. The remaining trade-off is time: peak arrives only after C2 has compiled hot paths with representative profiles. Workload classes differ — long-running services, short jobs, autoscaled fleets, serverless functions — and each deserves an explicit choice between default tiered JIT, AOT-cache-assisted warm-up, C1-only for very short runs, or Native Image.

10Interview Questions & Answers

Q1L1 · BasicConcept

What is tiered compilation?

Tiered compilation is HotSpot's default strategy, on since Java 8, of moving each method through a series of execution levels instead of choosing a single JIT compiler. Every method starts in the interpreter at level 0. Once its invocation and loop counters cross thresholds it is compiled by C1 at level 3, which runs much faster and records a detailed profile of branches, types and calls. If it stays hot, C2 recompiles it at level 4 using that profile. Levels 1 and 2 are C1 variants for trivial methods and for when C2 is busy. The result is quick warm-up from C1 and peak performance from C2 without any configuration.

Q2L1 · BasicConcept

What are C1 and C2?

They are HotSpot's two JIT compilers, built for opposite goals. C1, historically the client compiler, compiles in milliseconds using a simple SSA representation, constant folding, null-check elimination, limited inlining and linear-scan register allocation; its job is to replace slow interpretation quickly, and in tiered mode it also inserts profiling code. C2, the server or opto compiler, uses a sea-of-nodes graph and spends tens to hundreds of milliseconds per method on profile-guided inlining, escape analysis, loop optimisations, vectorisation, intrinsics and speculative optimisations guarded by uncommon traps. In a warmed-up service almost all CPU time is spent in C2 code, while C1 dominates the first seconds after startup.

Q3L2 · IntermediateInternals

What is the most common path a hot method takes?

The normal path is level 0, then level 3, then level 4. The method starts in the interpreter, which counts invocations and loop back-edges. When those counters cross the level-3 thresholds, by default around 200 invocations, or a combined count of about 2,000 including loops, the policy queues a C1 compilation with full profiling. The level-3 code then keeps counting and records branch frequencies, receiver types and call targets in the method's MethodData. After roughly 5,000 invocations, or 15,000 combined events, the method is queued for C2 and reaches level 4. Long-running loops can take the same route through OSR compilations. Thresholds also scale with compiler queue length.

Q4L2 · IntermediateInternals

Why are levels 1 and 4 called final?

Because no higher tier would improve them, so the policy never schedules another compilation. Level 4 is fully optimised C2 code with nothing above it. Level 1 is C1 code without profiling, used for trivial methods such as getters and constant accessors, where C2 could not generate meaningfully better code, and for methods that C2 cannot compile or has given up on. Since level 1 collects no counters, nothing would trigger a promotion anyway. Final does not mean permanent, though: if a speculative assumption fails or a class load invalidates an inlining decision, the code is deoptimised back to level 0, re-profiled and can climb the tiers again.

Q5L3 · Senior EngineerConcept

Why does tiered compilation profile in C1 code instead of in the interpreter?

Because profiling needs a large number of executions to be statistically meaningful, and C1 code collects them far faster and more cheaply. Level-3 code runs roughly an order of magnitude faster than the interpreter, so the same number of samples of branch frequencies, receiver types and call targets accumulates in a fraction of the wall-clock time and CPU. Meanwhile the application is already running reasonably fast code instead of crawling. That lets C2 compile hot methods sooner, with a profile of the same quality. The interpreter still keeps basic counters, and only gathers a full profile itself when C1 is lagging, for example when its queue is backed up.

Q6L3 · Senior EngineerInternals

When is level 2 used?

Level 2 is used when the C2 compile queue is long. Normally a warm method goes from the interpreter to level 3, but level-3 code carries full profiling instrumentation and is roughly 30 percent slower than plain C1 code. If C2 is far behind, a method could sit at level 3 for a long time paying that cost while its profile is already sufficient. So the policy compiles it at level 2 instead, C1 code with only invocation and back-edge counters, which runs faster while it waits. When the C2 queue shrinks, the method is recompiled at level 3 to collect a full profile and then promoted to level 4 as usual.

Q7L3 · Senior EngineerTrade-off

When would TieredStopAtLevel=1 make sense?

It makes sense for short-lived processes where C2 never pays back its cost: CLI tools, build steps, code generators, test runs and batch jobs that finish within seconds. Stopping at level 1 means methods are compiled once by C1 without profiling instrumentation, there are no C2 compilations competing for CPU, and less code-cache memory is used, so total runtime and CPU usage can drop. The trade-off is peak performance; C1 code can be several times slower than C2 code for hot loops. It is the wrong choice for long-running services, where it permanently gives up the optimisations that dominate steady-state throughput. Always confirm it with a measurement of end-to-end runtime.

Q8L3 · Senior EngineerInternals

How are compiler threads divided and why does it matter in containers?

HotSpot sizes CICompilerCount ergonomically from the number of available CPUs and assigns roughly one third of the threads to C1 and two thirds to C2, so a 16-CPU machine gets about 12 compiler threads. In a container the JVM sees the CPU limit, which often means only one or two vCPUs and very few compiler threads. During warm-up those threads compete with request-handling threads for the same throttled CPU quota, so C2 compilations queue up, methods linger at levels 2 and 3, and the time to peak performance stretches from seconds to minutes, often with CFS throttling visible. Give warm-up CPU headroom, use an AOT cache, or set the thread count explicitly after measuring.

Q9L4 · Technical ArchitectConcept

How can JDK 25 shorten the path to level 4 in production?

Through the AOT cache. In JDK 25 a training run with -XX:AOTCacheOutput, one step thanks to JEP 514, records not only loaded and linked classes from JEP 483 but also method profiles from JEP 515. A production JVM started with -XX:AOTCache finds profiles for hot methods immediately, so the policy can send them to C2 much earlier instead of waiting for level 3 to rebuild the same data. The cache does not contain compiled machine code; C2 still compiles at runtime, but sooner. The quality depends on the training run: it must exercise the real hot paths, ideally with production-like traffic, and must be regenerated whenever the application or JDK version changes.

Q10L4 · Technical ArchitectScenario

How would you evaluate whether a service's warm-up is limited by compilation?

I would first record JFR from JVM start through the first minutes of load, with compilation events enabled, and plot CPU usage, compiler queue lengths, compilation counts per level and the time when the hottest methods reach level 4. Next to that I would chart request latency. If latency stays high while compiler threads are busy, the queues are long, the container is CPU-throttled and hot methods reach C2 late, compilation is the bottleneck. If instead C2 code is in place early but latency is still high, the cause is elsewhere: class loading, lazy framework initialisation, cold caches or connection pools. A comparison run with extra CPU or an AOT cache confirms the diagnosis.

Q11L5 · Solution ArchitectDesign

How should compilation strategy be decided across an organisation's workloads?

I would define compilation defaults per workload class rather than per service, based on lifetime and latency sensitivity. Long-running services keep the default tiered JIT, which gives the best steady state. Frequently autoscaled or latency-sensitive services add an AOT cache trained in the CI pipeline on representative traffic, plus CPU headroom for warm-up. Short-lived jobs and tools may use C1-only, while scale-to-zero functions are candidates for Native Image when their libraries support it. Each default is validated with warm-up and steady-state measurements, written into an ADR and baked into base images and deployment templates, so teams inherit a sound setting and only deviate with evidence.

Test your knowledge on this topic →Flashcards from this topic's 11 questions, filtered by level and type.

Free question pack

Get 100 free Core Java interview questions

Subscribe for the Book 1 question pack (PDF, every answer explained) and new tutorials once a week. No spam, unsubscribe anytime. What is in the pack?

Almost done! Check your inbox and click the confirmation link. Your download link arrives as soon as you confirm.