JVM JIT & Tiered Compilation: Why Same Bytecode Runs 20x Faster When Compiled
This article explains JVM JIT tiered compilation (Tier 0-4), hotspot detection via dual counters, C1/C2 compiler roles, method inlining as optimization prerequisite, speculative optimizations with deoptimization fallback, and production practices for warmup, logging, CodeCache management, and troubleshooting compilation storms.
Introduction
The previous seven articles covered class loading, object allocation, and garbage collection. This article focuses on the true performance engine: why interpreted and compiled execution of the same bytecode can differ by 10-20x, and how the JVM makes "slow" code run faster over time. JIT is a subsystem that most Java engineers benefit from daily but rarely witness directly — post-release latency spikes, misguided parameter tuning halving throughput, and repeated recompilation CPU storms all originate here. After reading, you should be able to answer three production questions: why post-release warmup is necessary, why parameters like TieredStopAtLevel=1 must not be added blindly, and why method inlining determines the success of escape analysis (covered in article 06). Article 09 will cover diagnostic tooling.
Three Production Incidents
Incident 1: Post-Release 30-Second Latency Spike
A core service passed health checks immediately after release, but P99 latency jumped from 12ms to 400ms during canary ramp-up, recovering naturally after ~30 seconds. Enabling -Xlog:jit+compilation revealed the spike window coincided with a compilation peak — thousands of methods running interpreted while the compilation queue backed up. Interpreted execution's startup speed is the real cost of "cold" code : code must run before it qualifies for compilation. The fix is not GC tuning or scaling out, but release warmup — sending a small amount of real traffic through core paths after deployment to heat them up.
Incident 2: A "Faster Startup" Parameter Halves Throughput
To make containers pass readiness probes quickly, a developer added -XX:TieredStopAtLevel=1 (C1 only). Startup did improve, but load testing showed throughput cut in half with higher CPU — C2 was entirely disabled , leaving hot code stuck at C1's shallow optimization level. JIT parameters are not "free speed switches": each tier trades compilation time for runtime quality; cutting tiers removes the source of steady-state performance.
Incident 3: Unexplained High CPU Caused by Compiler Threads
A compute-intensive service showed persistently high CPU; flame graphs revealed business threads using only 70% CPU, with the rest consumed by C2 CompilerThread threads. Enabling -Xlog:deoptimization showed the same batch of methods repeatedly deoptimizing — a polymorphic field's actual type oscillated between two implementations, causing C2 to compile for type A, deopt on type B, recompile, deopt again. Speculative optimization betting wrong triggers compilation storms : this cannot be fixed by tuning parameters; the code's type oscillation must be eliminated.
These three incidents are three facets of the same subsystem: JIT liberates runtime performance from compile-time constraints, at the cost of moving some performance decisions to runtime — warmup solves "cold", tiering balances "fast vs good", deoptimization handles "wrong bets" . Troubleshooting mantra: first ask "Is this code running interpreted, C1, or C2 right now?" (tier), then "Has compilation kept up with traffic?" (queue), finally "Are optimizations being repeatedly overturned?" (deopt) — these three questions cover most JIT-related failures.
JIT Mental Model
The JVM is not a static artifact compiled once, but a dynamic system continuously "renovating" itself — code may transition from interpreted to C1 to C2 and back to interpreted; the same method may run different versions concurrently in different threads. Understanding this dissolves two common misconceptions: (1) "Benchmarks measure compiled performance" — they may only measure interpreted or C1 performance (JMH warmup iterations exist for this reason); (2) "Adding logs/breakpoints doesn't affect performance" — any bytecode change triggers re-climbing the tier ladder. JVM performance diagnosis must always start by confirming "Which version of the code am I seeing right now?"
Technical Analysis: How Code Gets Faster as It Runs
1.1 Three Execution Modes: From Full Interpretation to Full Compilation
Java code has three execution modes: Interpreted execution — the interpreter translates bytecode instruction by instruction; fast startup, no compilation wait, but slow execution (HotSpot's template interpreter maps each bytecode to a machine-code template, a high-performance interpreter). JIT compilation — at runtime, hot bytecode is compiled to machine code and cached in the CodeCache; subsequent calls jump directly to machine code, trading slow startup for high steady-state performance. AOT compilation — compile once at build time (native-image or GraalVM); instant startup, no JIT overhead, but loses runtime profiling, unable to perform aggressive optimizations based on "how it actually runs" — fast but dumb.
The three are not alternatives but a spectrum: Interpreter ensures "any code can run", JIT ensures "frequently run code runs fast", AOT ensures "fast from the start" . JDK 9's static AOT ( jaotc) was removed in later versions; the official direction shifted to CDS archives and Project Leyden (pre-startup optimization); GraalVM offers full AOT and pluggable Graal JIT. For most online services, tiered JIT + warmup + AppCDS (article 07) remains the cost-effective combination — AOT is a specialized solution for Serverless and CLI tools.
1.2 Tiered Compilation: Five Tiers Division of Labor
Since JDK 8, tiered compilation ( TieredCompilation) is enabled by default, with five tiers (Figure 1): Tier 0 — interpreted execution with profiling counters; Tier 1 — C1 compilation without instrumentation — fast-track for extremely short-lived hot methods; Tier 2 — C1 compilation with invocation counters — fallback when C2 queue is congested; Tier 3 — C1 compilation with full instrumentation (counters + type feedback) — primary data collection tier; Tier 4 — C2 deep optimization compilation — final destination for most hot code. The main path is 0 → 3 → 4 : use C1 to quickly stop bleeding while instrumenting to collect profiles, then hand off to C2 for polished optimization.
Two side paths are worth remembering: 3 → 2 downgrade — when C2 compilation queue backs up, new hot methods drop to Tier 2 (C1 with counters) to get a "good enough" version quickly, preserving timeliness; C2 version follows when queue clears — this is JIT's own "overload protection". Existence of Tier 1 — code that burns hot at startup may not survive long enough for a C2 compilation; direct C1 adoption is most economical. Tiered philosophy mirrors G1's pause prediction: use "good enough" to hold traffic immediately, use "better" to replace gradually — both reject one-shot perfection, embracing progressive approximation.
Compilation is performed asynchronously by background threads: Compiler thread count is determined by CICompilerCount based on CPU count (can be manually set; in containers, calculate based on actual quota). Compilation tasks are queued. This creates two runtime phenomena: business threads continue running interpreted versions during compilation (temporarily slower), switching to compiled version on next call (seamless); and the compilation queue has finite length — peak queuing prolongs "cold" time — which warmup (section 2.2) addresses.
1.3 Hotspot Detection: Two Counters
What counts as a "hotspot" is decided by two counters (Figure 2): Method invocation counter — increments on each method call; triggers compilation when reaching an internal threshold. Back-edge counter — increments on each backward branch (loop iteration); specifically captures "method called few times but loops wildly inside" hot loops. The sum of both counters reaching a combined threshold also triggers — exact thresholds are internal heuristics (different sets for Tier 3/4); business engineers need not memorize numbers, but must understand the mechanism .
Two design details define semantics: 1. Half-life decay — method counters halve every decay period — only "continuously hot" methods accumulate enough to promote; one-time startup code never gets compiled. This answers the common interview question "Why adjusting CompileThreshold has limited effect": under tiered compilation, threshold is just one of many heuristics; decay mechanism is the semantic core. 2. OSR (On-Stack Replacement) — when a hot loop triggers compilation, the method is still inside the loop — JVM plants a check at the loop back-edge; upon compilation completion, the current interpreted stack frame is replaced in-place with the compiled version, continuing execution without waiting for the next invocation. Without OSR, a batch service with a 10-minute loop would run entirely interpreted before speeding up — OSR is a lifeline for compute-intensive services.
1.4 C1 and C2: Relay of Fast and Good
C1 and C2 are two completely different compilers (Figure 3). C1 follows a lightweight pipeline: bytecode → HIR (high-level IR) → local optimizations (constant folding, dead code elimination) → LIR (low-level IR) → linear scan register allocation → code emission; completes in milliseconds, shallow optimizations but fast. C2 follows a heavyweight pipeline: consumes Tier 3's type feedback, performs method inlining, escape analysis (article 06), global optimizations, graph-coloring register allocation — tens of milliseconds to seconds, optimizing hot code to near hand-written assembly quality.
The relay key is instrumentation data flow : Tier 3's probes record "is this field actually Dog or Cat", "how many times has this branch been taken", "has this lock ever contended". C2 makes aggressive decisions based on these profiles — single-implementation virtual calls devirtualized and inlined, never-taken branches entirely pruned, uncontended locks eliminated. C1's temporary slowdown is deliberate: instrumentation overhead buys C2's optimization ammunition . This is the essence of tiered design — profiling collection and execution efficiency are split across two tiers, each doing its job.
1.5 Method Inlining: Mother of Optimizations
Inlining (Figure 4) copies callee code directly into caller — it is the prerequisite for all C2 optimizations: Escape analysis needs to see "object doesn't escape", which requires caller and callee in same compilation unit; constant propagation and dead code elimination similarly depend on inlining's flattened code view . Article 06 stated "EA's gains are downstream dividends of inlining quality" — this article supplies the other half: inlining is the first domino in the JIT optimization chain.
Decision chain has four gates: 1. Size gate — hot method inline threshold FreqInlineSize default 325 bytes; cold method MaxInlineSize default 35 bytes. 2. Heat gate — call frequency must be high enough to exceed threshold. 3. Devirtualization gate — virtual methods need type feedback confirming single implementation; multiple implementations only allow "guarded inlining" (with type check, failure triggers deopt). 4. Explosion radius gate — inline nesting depth ( MaxInlineLevel=9) and code size limit; JDK 18+ defaults DontInlineHugeMethods=on, rejecting methods with tens of thousands of bytecodes entirely — "Write small methods" is not just style, it directly determines whether code can be JIT-optimized .
Engineering mapping: every extra layer of dynamic proxy or AOP adds a breakpoint in the inlining chain — "small and direct" hot methods + less reflection is the greatest kindness to JIT. Spring applications' common performance trap (layered proxies reducing JIT gains) stems from here: Framework tax is an architectural decision, not something parameters can waive — on bottleneck paths, use plain method calls instead of proxies.
1.6 Optimization Toolbox: Beyond Inlining
C2's toolbox goes far beyond inlining; five cards most relevant in production: Lock Elision — escape analysis confirms lock object doesn't escape thread, synchronization removed entirely (local StringBuffer, local synchronized); Lock Coarsening — adjacent repeated lock/unlock on same lock merged into one; Loop Unrolling — replicate loop body to reduce branch and counter overhead, combined with vectorization (SIMD) turning scalar loops into vector instructions; Devirtualization — single-implementation virtual calls downgraded to direct calls or inlined; Constant Propagation & Folding — configuration constants propagated across methods, entire branches folded into single path.
A unified perspective: All these optimizations depend on "runtime profiles", and profiles can become stale — so every card has a corresponding revocation mechanism (deopt, see 1.7). JIT's aggressiveness and conservatism are two sides of the same coin: Dare to bet because you can undo; can undo because you dare to bet — understanding this makes "Why C2 needs profiling data" and "Why occasional deopt is normal" not isolated facts but two faces of one design.
1.7 Deoptimization: What Happens When the Bet Is Wrong
Speculative optimizations have four guarded premises (Figure 5): type assumptions, branch assumptions, lock assumptions, null assumptions. When a premise breaks, deoptimization triggers, three steps: Discard — this machine code is invalidated; Fallback — current stack frame returns to interpreter, resuming from nearest safepoint (correctness preserved); Re-climb — counters reset, recompile based on new observations. Unlike class loading's erroneous state (article 07), deopt is a recoverable tactical retreat — a method deoptimized multiple times just recompiles repeatedly, no death sentence.
Two abnormal patterns to recognize: Occasional deopt — normal during early deployment when type distribution hasn't stabilized; ignore. Recompilation storm — same batch of methods repeatedly "compile, deopt, recompile, deopt", CPU heavily consumed by compiler threads, business jitter. Typical root cause: type oscillation in code — a collection sometimes holds A, sometimes B; or a field has different types on different paths. Remediation is code change, not parameter suppression : converge type branches, split methods with same signature but different responsibilities.
Connecting 1.2–1.7 reveals a hidden mainline: JIT is a complete "Observe-Decide-Execute-Correct" closed loop — counters observe (1.3), policy decides (3.1), C2 executes optimizations (1.4–1.6), deopt corrects (1.7); four links interlock, missing any collapses the system. This mainline also structures article 09's diagnostic toolchain: when troubleshooting JIT, first ask which link broke — "Unobserved" (warmup missed), "Decision overloaded" (compilation queue blocked), "Optimization invalidated" (inlining broken), "Repeated correction" (deopt storm) — four branches map to four completely different fixes.
Production Practice: Observability, Warmup, and Governance
2.1 JIT Observability Full Configuration
A production-ready JIT observability config — principle: Lightweight by default, full verbosity only during incidents :
# ==== JIT Observability (JDK17) ====
# Normal: Compilation Activity Summary (small volume)
-Xlog:jit+compilation=info
:file=/logs/jit.log:filecount=2,filesize=20m
# Output line: tier level, method name, duration
# nmt-style seq 4 b method (code size)
# Incident A: Full Deoptimization Record
-Xlog:deoptimization=info
:file=/logs/deopt.log:filecount=2,filesize=20m
# Key column: reason= type/branch/null
# Incident B: Compilation Details (large volume, NOT for normal)
# -Xlog:jit+compilation=debug
# Legacy: -XX:+PrintCompilation
# CodeCache Watermark (for monitoring)
-XX:ReservedCodeCacheSize=240m
# JVM used/max via jcmd VM.native_memory
# Or GC log codecache segmentThree reading rules: 1. Check compilation coverage — after warmup, spot-check core methods reached Tier 4 (tier marker in logs); core paths stuck at low tiers means compilation queue hasn't caught up. 2. Check deopt reason distribution — unstable_if, type_confirmed concentrated on same methods = Incident 3's recompilation storm scene. 3. Watch CodeCache watermark — nearing limit causes JVM to reject new compilations (flush old code or stop compiling), manifesting as "service runs days then performance quietly degrades" — CodeCache full is a more insidious performance regression source than GC.
An often-overlooked linkage: CodeCache and Class Unloading . In hot-deploy/plugin environments, compiled code of unloaded classes must also be evicted from CodeCache — article 07's class unloading three gates equally constrain JIT artifacts. So "CodeCache only rises, never falls" is often a downstream symptom of classloader leaks; both logs ( class+unload and jit) must be viewed together.
2.2 Startup Warmup: Curing the "First 30-Second Spike"
Warmup goal is precise: Let core path methods complete the 0 → 3 → 4 ladder climb before real traffic arrives . Three-piece kit ordered by cost-effectiveness:
# ==== Warmup Three-Piece Kit ====
# 1. Release Hook: Self-invoke Core APIs Before Ready
# (K8s preStop before readiness gate)
# curl with real business params hitting core APIs
# Warmup N rounds, 500ms interval each
# 2. Canary Ramp: Small Traffic Staircase
# 1% -> 5% -> 25% -> 100%
# Each step observe P99 recovery before next
# 3. In-App Warmup Task (ApplicationRunner)
# Post-start thread pool concurrently invoke internal methods
# Coverage: core serialization, hot cache reads,
# regex and reflection first executionsThree engineering details determine warmup success: 1. Params must be real — warming up with empty or extreme params gives JIT a type profile mismatching real traffic, wasting compilation or even inducing deopt — warmup requests must follow real business param distribution. 2. Coverage must be accurate — warm up "core path methods", not all APIs — pull top call chains from APM to decide warmup list. 3. Acceptance must be quantitative — after warmup, use JIT logs to confirm core methods reached Tier 4, not "feels about right".
One often-missed warmup target: Hot loops need OSR safety net . Core paths with "single method millions of iterations" batch logic (bulk parsing, large collection computation) may not trigger promotion via normal calls in time — warmup scripts should execute such tasks once at medium scale, letting back-edge counter trigger OSR compilation; a fixed item for compute-intensive services. Also note warmup-to-canary sequencing: complete warmup before ramping, otherwise spike is merely spread over canary head instead of eliminated — Warmup acceptance metric is "P99 no spike at ramp-up instant", not "warmup ran without error" .
System-level addition: Spring Boot 3.2+ CRaC (Checkpoint Restore) and Project Leyden direction — snapshot warmed-up process state as checkpoint, subsequent starts restore directly to "already hot" state. This "warm once, benefit everywhere" ultimate form costs environment binding of checkpoint (file handles, network connections need restoration). Current production recommendation remains "Warmup Three-Piece Kit + AppCDS" combo, CRaC as evolution option for JDK 21+ environments.
2.3 Parameter Red Lines and CodeCache Governance
JIT parameter discipline: Touch few, know where red lines are :
# ==== JIT Parameter Red Lines ====
# Forbidden:
# TieredStopAtLevel=1 Cuts off C2
# (Incident 2: Steady-state throughput halved)
# -Xint Full interpretation (temporary diagnosis only)
# CompileThreshold Limited meaning under tiering
# Cautious (scenario-specific):
# CICompilerCount=N When container CPU constrained
# Manually set compiler thread count
# ReservedCodeCacheSize=256m
# Increase for large apps with CodeCache pressure
# Observability Switches (temporary, on-demand):
# -XX:+PrintCompilation
# -XX:+UnlockDiagnosticVMOptions
# (with PrintInlining to see inlining decisions)CodeCache governance three steps: 1. Measure watermark — jcmd VM.native_memory or CodeCacheMXBean get used/total, run a week at peak to see curve. 2. Attribute — sustained growth investigate classloader leak (linkage in 2.1); normal fluctuation no action. 3. Safety net — 240M default sufficient for most services; ultra-large apps (thousands of hot methods + many lambdas/dynamic code) raise to 256-320M, simultaneously monitor whether container memory limit squeezes reservation — CodeCache is another off-heap ledger, a hidden suspect for container OOMKilled (part of article 01's container memory accounting).
Core Source Code: Four Truths of JIT
3.1 Tier Promotion Decision: TieredThresholdPolicy
Core logic of tiered strategy (simplified, C++ source AdvancedThresholdPolicy):
// Simplified: TieredThresholdPolicy::compile
// (Methodology sketch, not line-by-line)
void suggest_compile(method, level) {
// Main path submit: Tier3 collects data
if (invocation + backedge
>= tier3_threshold(method)) {
submit(method, TIER_3); // C1 full instrumentation
}
// Data ready + sustained heat -> promote to C2
if (profiling_complete(method)
&& invocations >= tier4_min) {
submit(method, TIER_4); // C2
}
// Queue congestion protection: C2 queue too long
// New hot methods downgrade to Tier2 first
if (queue_backlog > limit
&& !urgent(method)) {
submit(method, TIER_2);
}
}Three segments correspond to three paths: Tier3 collection, Tier4 promotion, Tier2 downgrade for timeliness. Key reading point: "Downgrade is not anomaly but design" — when asked in interviews "Why does my method log show Tier2?", answer is usually C2 queue was busy at that moment, not config error. True identity lies in AdvancedThresholdPolicy, principle is "Threshold heuristics + queue watermark feedback".
3.2 Counters and Half-Life: Hotspot Determination
Method counter half-life mechanism (simplified):
// Simplified: MethodCounters Decay
// Each decay period: count >>= 1
void decay_counters() {
// Invocation counter half-life
_invocation_count
= _invocation_count >> 1;
// Back-edge counter same half-life
_backedge_count
= _backedge_count >> 1;
}
// Semantic effect:
// Sustained hot: increments outpace decay, accumulates
// One-shot hot: decay catches up, never promotes
// This is the mechanism root of "Startup flash code not compiled"
// Not because threshold high, but decay counteractsTwo bit-shifts implement "heat must be sustained" semantics — cheaper than any complex formula. This also explains why warmup works: Warmup uses artificially high-frequency calls to force accumulation past decay counteraction ; low-frequency core interfaces (e.g., scheduled entry called ten times a day) will never reach C2 — such methods' performance issues require AOT/archive or acceptance, not parameter tuning.
3.3 Inlining Decision: ok_to_inline's Four Gates
C2 inlining decision skeleton (simplified, real body InlineTree::check_can_inline):
// Simplified: InlineTree Inlining Judgment
not_inline_reason check(callee, site) {
if (depth > MaxInlineLevel)
return TOO_DEEP; // Nesting limit exceeded
if (callee.bc_size
> hot ? FreqInlineSize
: MaxInlineSize)
return TOO_BIG; // Size limit exceeded
if (DontInlineHugeMethods
&& callee.is_huge())
return HUGE; // Huge method rejected
if (callee.is_virtual()
&& !can_devirtualize(site))
return NEED_GUARD; // Guarded inlining
return INLINE_OK;
}
// NEED_GUARD still inlinable: insert type check,
// Check failure -> uncommon trap -> deoptFour gates correspond one-to-one with 1.5's decision chain. NEED_GUARD is key to understanding "Optimization and Correctness Coexist" : multiple implementations don't prevent inlining, but inlined code carries a type guard — bet right take fast path, bet wrong trigger uncommon trap. This also links to 3.4: deopt is not independent mechanism, it's the breach exit of speculative optimization.
3.4 Uncommon Trap: Breach Exit
Deoptimization trigger points (simplified):
// Type Guard in Compiled Code (Sketch)
if (obj->klass() != Dog_klass) {
// Bet wrong: trigger uncommon trap
deopt(frame, reason=type_confirmed);
// Internal three steps:
// 1. Mark this compiled code invalid (not entrant)
// 2. Current frame falls back to interpreter, resume from safepoint
// 3. Counters reset, resubmit compilation based on new profile
}
// Common reasons:
// null_check Null assumption broken
// unstable_if Branch assumption broken
// class_load New type appeared
// constraint Constraint invalidatedEngineering detail: Invalidated code is not immediately deleted but marked not entrant — threads already inside can finish; new calls go interpreter. CodeCache not entrant code waits for all active frames to exit before reclamation — one source of CodeCache watermark spikes. Feeding reason field into monitoring (2.1) turns deopt storm localization from "guess from logs" to "group by reason and sort".
Seven Accounts: JIT Governance Cost-Benefit
Startup Account: Warmup + AppCDS compress release unavailability window from "natural recovery 30 seconds" to under 5 seconds; high-frequency release teams save far more in spike losses than configuration cost — Incident 1's correct solution.
Throughput Account: Tiered compilation delivers 10-20x over interpretation at steady state, while "enabled by default, zero config" — biggest gain is "do nothing". Only one thing to do: don't break default config (Incident 2).
Memory Account: CodeCache default 240M is independent off-heap ledger; container memory limit must cover it — large apps raising limit must also adjust container limit, else OOMKilled blames GC.
CPU Account: Compiler threads consume CPU for steady-state performance; capacity planned for "compilation peak + steady state"; compilation storm (Incident 3) is second top suspect for CPU anomalies (first is GC, article 03).
Time Account: Code discipline "small methods, less reflection, less proxies" near-zero cost, yet decides inlining and EA ceiling — articles 06/08 shared conclusion: good code is JIT's first parameter.
Risk Account: Occasional deopt is designed-in cost; recompilation storm is code smell developer — feeding reason distribution into monitoring turns risk from "mystical jitter" into "groupable metrics".
Habit Account: Release warmup in release checklist, JIT parameters in "red line list" governance, CodeCache watermark in monitoring dashboard — three disciplines turn JIT-class failures from "occasional explosion" to "won't explode".
Seven accounts combined judgment: JIT governance is "Default Correct, Humans Easily Break" typical — 90% value in guarding defaults and doing warmup, remaining 10% in reading logs . Mirrors GC governance (article 03 "Tuning Live Object Scale"): GC's work is code-side allocation discipline, JIT's work is code-side method shape — two subsystems different paths, same destination.
High-Frequency Interview Questions (with Answers)
Q1: What is JIT? Why is it needed?
Runtime mechanism compiling hot bytecode to machine code and caching. Interpretation starts fast but runs slow; static compilation fast but loses runtime profiling — JIT compromises with "run first collect profile, then compile", letting hot code approach static compilation performance while retaining interpretation flexibility.
Q2: What do the five tiered compilation levels do?
Tier0: Interpret + count; Tier1: C1 no instrumentation (short-lived hot methods fast in/out); Tier2: C1 with counters (queue congestion fallback); Tier3: C1 full instrumentation (primary data collection); Tier4: C2 deep optimization (destination). Main path 0→3→4.
Q3: How does JVM decide a method is hot?
Two counters: Invocation Counter (increment per call) and Back-Edge Counter (increment per loop back-edge); sum reaches combined threshold triggers compilation. With half-life decay: counters halve periodically, only "continuously hot" methods promote — filters one-time startup code.
Q4: What is OSR? What problem does it solve?
On-Stack Replacement: When hot loop triggers compilation but method hasn't returned, JVM plants check at loop back-edge; after compilation, current interpreted stack frame replaced in-place with compiled version continuing execution. Solves "Long loop must finish current invocation before benefiting from compilation" — critical for batch/compute-intensive services.
Q5: Difference between C1 and C2?
C1 Lightweight: HIR/LIR two-level IR + Linear Scan Register Allocation, millisecond codegen, shallow optimizations; C2 Heavyweight: Consumes type feedback for Inlining/Escape Analysis/Global Optimizations + Graph Coloring Allocation, second-level codegen, high quality. Tier3 instrumentation feeds C2 profiles — C1's temporary slowdown is intentional design.
Q6: Why is Method Inlining called "Mother of Optimizations"?
Inlining expands callee into caller, giving Escape Analysis, Constant Propagation, Dead Code Elimination cross-method visibility — without inlining, these optimizations almost entirely lose effect. Article 06's Scalar Replacement, Lock Elision all require inlining as prerequisite.
Q7: What are Inlining Thresholds and Limits?
FreqInlineSize=325(hot), MaxInlineSize=35 (cold), MaxInlineLevel=9 (nesting depth), InlineSmallCode=2000 (already compiled), JDK18+ DontInlineHugeMethods rejects huge methods. Virtual methods need devirtualization or guarded inlining.
Q8: What is Guarded Inlining? What happens on failure?
Multi-implementation virtual method inlined per current type with type check guard; runtime type mismatch triggers uncommon trap — compiled code discarded, stack frame falls back to interpreter, recompile. "Bet right fast path, bet wrong has exit".
Q9: What is Deoptimization? What is the Process?
Rollback mechanism triggered when speculative optimization premises (Type/Branch/Lock/Null assumptions) break. Three steps: Machine code marked invalid (not entrant), current frame falls back to interpreter from safepoint, counters reset and recompile per new profile. Correctness preserved, occasional is normal.
Q10: Common Deoptimization Reasons and Meanings?
null_check(Null assumption broken), unstable_if (Never-taken branch taken), type_confirmed / class_load (Type assumption broken or new type appeared), constraint (Constraint invalidated). Same batch repeatedly triggering = recompilation storm; root cause often code type oscillation; fix by code change.
Q11: Why Not Tune CompileThreshold?
Under tiered compilation, hotspot determination uses multiple internal heuristic thresholds + half-life decay; CompileThreshold is just one and semantics changed (different threshold sets per tier). Tuning it neither precisely controls compilation timing nor avoids interfering with queue watermark protection — understanding mechanism beats memorizing parameters.
Q12: What Does -XX:TieredStopAtLevel=1 Do? What's the Cost?
Stops compilation at C1 level, faster startup, less compilation overhead — suitable for short-lived processes (CLI tools, build scripts). Cost: C2 removed, steady-state throughput significantly drops (up to halved) — forbidden for online services. Same red line: -Xint full interpretation.
Q13: What is CodeCache? What Happens When Full?
Off-heap memory storing JIT compiled artifacts, default 240M (tiered). When full, JVM rejects new compilations or preferentially flushes old code, manifesting as performance quietly degrading after days of running. Governance: Monitor watermark, investigate classloader leak downstream effects, large apps raise limit and sync container quota.
Q14: Post-Release P99 Spike Then Stabilize — How to Locate and Fix?
Locate: JIT logs show compilation activity during spike window, confirm interpreted start + compilation queue backup. Fix: Release Warmup (Real params self-invoke core APIs) + Canary Staircase Ramp + AppCDS. Acceptance: Core methods confirmed Tier4 after warmup.
Q15: Why Must Warmup Use Real Parameters?
JIT's aggressive optimizations based on runtime profiles (Actual field types, Branch directions); Empty/Extreme params collect profiles mismatching real traffic — Compiled code assumptions wrong, post-release frequent deopt recompile, warmup benefit zero or negative.
Q16: How Is Compiler Thread Count Decided? When to Manually Adjust?
Default auto by CPU cores ( CICompilerCount), container env by actual quota. Manual adjust scenarios: CPU-quota-constrained large apps (Compiler threads crowd business threads) reduce; Compute-intensive with severe warmup queue backlog moderately increase — generally leave alone.
Q17: JIT vs AOT Trade-offs?
JIT has runtime profile for aggressive optimization, cost: Slow startup + Compilation overhead; AOT Instant startup, Low memory, but No profile, Conservative optimization, Loses dynamic class loading/reflection (GraalVM native-image needs config). Online long-running services use JIT+Warmup; Serverless/CLI use AOT.
Q18: Graal JIT vs C2 Relationship?
Graal is Java-written JIT compiler, can replace C2 via -XX:+UseJVMCICompiler (JDK17+ needs compiler dependency). Advantage: Better inlining and vectorization scenarios; C2 is default and most mature. They are "Pluggable Compiler Backend" relationship, Tiered Framework unchanged.
Q19: What is Method Inlining's "Method Shape" Problem?
Reflection, Dynamic Proxy, AOP layered wrapping breaks inlining chain at every layer, JIT optimization cannot unfold — On performance-sensitive paths in heavy frameworks, replace proxy layers with plain method calls. This is same coin as Article 06's Escape Analysis Failure List.
Q20: How to Discover "High CPU but Normal GC" Is Compilation?
Three steps: 1. Thread names confirm C1/C2 CompilerThread CPU share ( top -H); 2. -Xlog:deoptimization group by reason to see storm; 3. -Xlog:jit+compilation watch compilation queue churn (Repeated submission of same methods). Fix: Converge Type Oscillation, Split Methods — Not Parameter Suppression.
One-Page Cheat Sheet
JIT Nine Sentences: 1. Three Execution Modes: Interpreter Safety Net, JIT Speedup, AOT Startup Guarantee; 2. Tiered Compilation 0→3→4 Main Path, Tier3 Collects Data Tier4 Produces Quality; 3. Two Counters + Half-Life = Only Sustained Heat Promotes; 4. OSR Lets Hot Loops Switch In-Place, No Need to Wait for Method Return; 5. C1 Paramedic Millisecond Codegen, C2 Construction Crew Precision Build, Instrumentation is Intelligence; 6. Inlining is Mother of Optimizations: Methods Small, Chain Straight, Few Proxies — Three Numbers 325/35/9; 7. Guarded Inlining Bets Right Fast Path, Bets Wrong Deopt Fallback Reclimb; 8. CodeCache 240M Is Another Off-Heap Ledger, Full Causes Silent Performance Decay; 9. Warmup Three-Piece Kit: Real Params Self-Hit + Canary Staircase + In-App Warmup.
Summary and Next Article Preview
Reviewing three incidents: Release spike answer is Warmup not Scale-out; "Faster Startup" answer is Guard Tiering not Cut Levels; Compilation Storm answer is Converge Type Oscillation not Suppress Parameters. Complete JIT Governance Methodology in One Sentence: JIT Performance Is "Run Out" Not "Configured Out" — Default Config Already Near Optimal, Human Value Lies in Warmup Controlling "Cold", Discipline Guarding "Tiers", Observability Watching "Storms" . Code-side Method Shape (Small, Straight, Few Proxies) Stands Alongside Article 06's Allocation Discipline as Other Half: Good Code Forever Is Biggest Performance Parameter — This Sentence Verified Across Three Subsystems In This Series.
Next Article Enters "Diagnostic Toolchain": Proper Usage of jstat/jstack/jmap/jcmd Four Commands, Arthas Online Diagnosis, JFR Flight Recorder Recording and Interpretation, Thread Dump State Machine Analysis — Giving Unified Unboxing Toolbox For All "Problem Scenes" Accumulated In Previous Eight Articles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
