Java Core & JVM (50 Qs)•Part 3: JVM Internals, Low-Latency Systems & Production Tuning (25 - 35)
How does the JIT Compiler work (Tiered Compilation C1 vs C2) and what is Escape Analysis?
senior LevelGolang
In Simple Words: The JVM starts executing code via the Interpreter for instant startup. As methods are called thousands of times ("hot spots"), the JIT (Just-In-Time) Compiler compiles bytecode directly into native machine code (x86/ARM instructions).
Tiered Compilation Architecture:
- Level 0 (Interpreter): Raw bytecode interpretation.
- Level 1-3 (C1 Client Compiler): Fast native compilation with basic optimizations and profiling hooks.
- Level 4 (C2 Server Compiler): Heavy, aggressive native optimizations including loop unrolling, speculative devirtualization, and method inlining.
Escape Analysis:
The C2 compiler analyzes whether an object allocated within a method escapes outside its scope (e.g., returned or passed to another thread). If the object does not escape:
- Scalar Replacement: The JVM eliminates the heap allocation entirely and maps object fields directly to CPU hardware registers!
- Lock Elision: If a
synchronizedblock locks an object that only one thread can ever see, the lock is completely stripped out.