Video in progress
How do the C1 and C2 compilers in HotSpot differ, and how does tiered compilation work?
C1 compiles fast but optimizes little; C2 is slower but optimizes aggressively based on the profile. Tiered compilation runs a method first through the interpreter, then C1 (collecting a profile), and through C2 if it gets hot enough.
📖 Answer
Since Java 8 tiered compilation has been on by default. A simplified path:
| Level | Who | What it does |
|---|---|---|
| 0 | interpreter | collects a profile |
| 1-3 | C1 | fast native code; at level 3 with a full profile |
| 4 | C2 | inlining, escape analysis, loop optimization |
In other words, C1 gets the application "alive" sooner, and C2 gives a long-running server its final speed.
C2 relies on the profile and makes assumptions: "only ArrayList has ever implemented this interface". If the assumption breaks later (a LinkedList shows up), a deoptimization happens: the code falls back to the interpreter and is compiled again.
Practical points:
-XX:TieredStopAtLevel=1runs with C1 only: startup is faster for local dev or short CLI tools, but long runs are slower;- compilation runs on separate threads, so in a container with little CPU the warm-up stretches noticeably;
- in GraalVM the Graal JIT can be used instead of C2.
To watch the compilation process: jcmd <pid> Compiler.queue or the Compilation events in JFR.
Sign up to mark studied questions and save your progress.