Module 1: JVM Architecture & Class Loading
1.2 JIT Compilation, Tiered Compilation & C2 Optimizations
Estimated reading & practice time: ~20 minutes
Learning Objectives
• Explain HotSpot's 5 tiers of compilation (0=Interpreter, 1-3=C1 Client, 4=C2 Server).
• Identify HotSpot method inlining and loop unrolling.
• Understand deoptimization (deopt) when polymorphic call assumptions fail.
Mental Model & Analogy
Tiered compilation is like learning a new song on guitar: First, you slowly read the sheet music note by note (Interpreter). After practicing 5 times, you remember the chords (C1). After 1,000 repetitions, your fingers play automatically at light speed without looking at the sheet (C2 optimized machine code).
Core Concept Breakdown
The HotSpot JVM achieves peak performance via **Tiered Compilation**:
1. **Tier 0 (Interpreter)**: Runs bytecode immediately with zero compilation pause while collecting execution profiling statistics.
2. **Tiers 1-3 (C1 Client Compiler)**: Compiles frequently executed methods into machine code quickly with basic optimizations.
3. **Tier 4 (C2 Server Compiler)**: Applies aggressive optimizations: method inlining, dead code elimination, loop vectorization, and escape analysis.
4. **On-Stack Replacement (OSR)**: Replaces an interpreted long-running loop with compiled machine code in the middle of its execution!
Architecture Diagram
Code Example
JAVA Example
public class JitWarmupDemo {
public static long calculateSum(int iterations) {
long sum = 0;
for (int i = 0; i < iterations; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
// Warmup loop: triggers C1/C2 JIT compilation
for (int i = 0; i < 20_000; i++) {
calculateSum(100);
}
long start = System.nanoTime();
long result = calculateSum(1_000_000);
long elapsed = System.nanoTime() - start;
System.out.println("Sum: " + result + " in " + (elapsed / 1000) + " µs (C2 Compiled)");
}
}Line-by-Line Walkthrough
• Running 20,000 iterations surpasses the HotSpot JIT threshold, triggering Tier 4 C2 native compilation.
Sum: 499999500000 in 340 µs (C2 Compiled)
Common Pitfalls & Debugging
• Benchmarking Java methods using `System.nanoTime()` without a warmup phase: you end up measuring slow bytecode interpreter overhead rather than compiled production speed. Always use JMH.
Try It Yourself — Java Sandbox
Live ExecutionRun a Java app with JVM flag `-XX:+PrintCompilation` to observe HotSpot compiling methods live in your terminal.
Solution.java
Edit the code and click Run Code to verify output directly inside TBLearn.
1
Terminal Output
Click "Run Code" or press Ctrl+Enter to execute.
Knowledge Checkpoint • Earn +40 Credits
Test Yourself Before Moving Forward
Short, practical questions tied directly to this section. Passing awards Credits toward course discounts and interview packs.
Module 1 Checkpoint: JVM ClassLoading & Tiered JIT
Test understanding of class loader delegation hierarchies, C1/C2 JIT compilation, and tiered HotSpot optimization.