Quick revision
The short answer
C2 compiled the hot methods speculatively, treating the never-taken branches as uncommon traps and assuming the receiver types and single implementations it had seen. When the feature flag turned on the new path, every hot method hit those traps, so the JVM deoptimised frames to the interpreter, made the compiled code not entrant and recompiled with new profiles, and new classes loaded by the feature also broke class hierarchy assumptions. A short spike after a change is normal; repeated deoptimise and recompile cycles for the same methods are the real problem, and JFR's deoptimisation events show which.
What to remember
In this answer, we saw that C2 compiles hot methods speculatively, treating never-taken branches as uncommon traps and assuming the types and single implementations it has seen. The feature flag sent traffic into those traps in many hot methods at once, and the new classes broke class hierarchy assumptions, so the JVM deoptimised, made the code not entrant and recompiled with new profiles, slowing requests for a few seconds. One burst is normal; repeated cycles for the same methods are the problem. JFR deoptimisation events show which, and a gradual rollout of the flag softens the spike.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
C2 compiled the hot methods speculatively, treating the never-taken branches as uncommon traps and assuming the receiver types and single implementations it had seen. When the feature flag turned on the new path, every hot method hit those traps, so the JVM deoptimised frames to the interpreter, made the compiled code not entrant and recompiled with new profiles, and new classes loaded by the feature also broke class hierarchy assumptions. A short spike after a change is normal; repeated deoptimise and recompile cycles for the same methods are the real problem, and JFR's deoptimisation events show which.
Why this question is asked
Feature flags let us change behaviour without a deploy, which is great, but from the JIT's point of view a flag flip is a deploy of new behaviour into code that was optimised for the old behaviour. The latency spike in this scenario is the JVM paying for that. Interviewers ask it to see whether we can connect a production symptom to speculative compilation and know when to worry. In this answer, we will recap how C2 speculates, walk through what happens when the flag flips, see the class loading angle, draw the line between normal and harmful, and look at how to diagnose and soften the spike.
1A Quick Recap of Speculative Compilation
C2, HotSpot's optimising compiler, uses the profile collected while the method ran at a lower tier. If a branch was never taken, C2 does not compile it; it places an uncommon trap there, a stub that hands control back to the interpreter if the branch is ever reached. If a call site saw only one receiver type, C2 inlines that type's method behind a type check that traps on any other type. If only one class implements an interface, class hierarchy analysis lets C2 call it directly, with a dependency recorded so the code is invalidated if another implementation is ever loaded.
These speculations remove code, calls and checks, which is a large part of why C2 code is fast. They are safe because every one has a way back.
2What Happens When the Flag Flips
Before the flag, the new path was dead code. The branch if (flags.newPricing()) was never taken, so in every hot method that contains it, C2 compiled an uncommon trap instead. The moment the flag turns on, the next request reaches that trap in each of those methods.
6 messages in order. Play them, or step through them one at a time.
Every hot method that contains the flag check goes through this at about the same time.
At each trap, the JVM deoptimises the running frame, converting it into an interpreter frame, and makes the compiled method not entrant. Requests now run those methods in the interpreter or in C1 code, which is several times slower, until the methods are hot again and C2 has recompiled them with a profile that includes the new branch. With many hot methods affected at once, the compiler queue fills, and the service runs slower code for several seconds. That is the latency spike.
3Class Loading Breaks Assumptions Too
The new feature probably brings new classes. Suppose PricingEngine had only one implementation loaded, LegacyEngine. C2 used class hierarchy analysis to call LegacyEngine.price() directly, maybe inlined. When the flag loads NewEngine, a second implementation, the JVM checks the dependencies recorded by compiled methods and makes every method that assumed a single implementation not entrant, even methods that never call the new path. This is why the burst of recompilation can be wider than the code the flag actually touches.
4Normal Spike or Real Problem?
A single burst of deoptimisation and recompilation after a change is normal. The JVM learned something new about the program and adapts, and the spike lasts a few seconds. It becomes a real problem when the same methods are deoptimised and recompiled again and again. That happens when behaviour keeps alternating, for example a flag toggled per request or per tenant so a branch looks rare in one profile and common in the next, or a call site that keeps seeing new receiver types. Each cycle costs compile CPU and time in slow code, and after too many recompilations the JVM may give up on the method and leave it interpreted, made not compilable, which is much worse.
| Signal | Normal adaptation | Real problem |
|---|---|---|
| Timing | one burst after a change | repeats for the same methods |
| Duration | a few seconds | ongoing or periodic |
| Methods | many, once each | a few, many times |
PrintCompilation | made not entrant, then new level 4 | cycles, or made not compilable |
5Diagnosing and Softening the Spike
JFR records a jdk.Deoptimization event for each deoptimisation, with the method, line number, bytecode index, reason (such as unstable_if for a branch that was assumed never taken, or class_check for an unexpected type) and action (such as reinterpret or maybe_recompile). Grouping those events by method shows whether we had one burst or a cycle.
To soften a one-time spike, we warm the new path before full traffic: enable the flag for a small percentage of requests first, so the JVM sees the new branch and recompiles while most traffic still flows through fast code, then ramp up. Rolling the change out instance by instance has the same effect at the fleet level. For a cycle, we fix the code shape, for example by evaluating a per-request flag in a separate method so the hot method's profile stays stable, or by keeping polymorphism out of the hottest call sites.
6Follow-up questions
Would turning off the flag again fix the spike?
It would cause another round. The recompiled code now includes the new branch, so turning it off is cheap at first, but the profile will gradually show the branch as rare again, and any later flip may trap again. Flipping back and forth is exactly the pattern that leads to repeated cycles.
Could we avoid speculation for that branch entirely?
Not directly in a supported way, and we would not want to. Speculation is what makes the common case fast. The better approach is to let the JVM see the new behaviour gradually with a percentage rollout, so the adaptation happens while the impact is small.
Does the AOT cache in JDK 25 help here?
Only partly. The AOT cache stores profiles from a training run, which helps warmup after startup. A flag flip in a long-running process is new behaviour the training run did not see, so the JVM still has to adapt. If the training run included the new path, though, restarted instances would start with a profile that already contains it.
7Conclusion
In this answer, we saw that C2 compiles hot methods speculatively, treating never-taken branches as uncommon traps and assuming the types and single implementations it has seen. The feature flag sent traffic into those traps in many hot methods at once, and the new classes broke class hierarchy assumptions, so the JVM deoptimised, made the code not entrant and recompiled with new profiles, slowing requests for a few seconds. One burst is normal; repeated cycles for the same methods are the problem. JFR deoptimisation events show which, and a gradual rollout of the flag softens the spike.
Open with
What happened is speculative compilation meeting new behaviour. Before the flag, the new code path was never executed, so when C2 compiled the hot methods it used the profile and compiled that branch as an uncommon trap, and it inlined the receiver types it had seen.
Then, if they want more
If the feature also loads new classes, class hierarchy analysis assumptions break too, for example an interface that had a single implementation now has two. When the flag flips, requests hit those traps in many hot methods at once.
The JVM deoptimises the frames back to the interpreter, marks the compiled code not entrant, and the methods have to be profiled and recompiled with the new branch included. For a few seconds a lot of hot code runs interpreted or in C1, and the compiler queue is full, so latency spikes. A single burst like that is normal adaptation.
It becomes a real problem if the same methods are deoptimised and recompiled repeatedly, for example with a per-request flag that keeps changing what the profile sees, and eventually the JVM may stop compiling them. I would check JFR's jdk.Deoptimization events grouped by method and reason, like unstable_if or class_check.
To reduce the spike, I would roll the flag out gradually, a small percentage first, so the JVM adapts while most traffic still runs fast code.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Right after a feature flag enables a new code path in a hot service, latency spikes for...": https://scrivio-interview-library.netlify.app/java/jit-compilation/700344e8ca/