Scrivio Interview library
InternalsIntermediate8 min read · updated 12 November 2026

Name the JIT optimisations that matter most for Java performance and the code patterns that defeat them.

The JIT optimisation that matters most is inlining, because it removes call overhead and lets every other optimisation see more code at once. Escape analysis then removes allocations and locks for objects that never leave a method, devirtualization turns interface and virtual calls into direct ones, and loop optimisations remove range checks, unroll and vectorize. Megamorphic call sites, huge methods and types that keep changing, which cause deoptimization, are the patterns that defeat them.

Why this question is asked

Java's speed comes mostly from the C2 JIT compiler, not from the code we write line by line. When a service is slower than expected, the cause is often a pattern that quietly stops the JIT from doing its best work. Interviewers ask this question to see whether we understand what the JIT relies on, so that we can write code it optimizes well and recognize the cases where it gives up. In this answer, we will recap what the JIT knows about our program, then go through inlining, devirtualization, escape analysis and loop optimisations, and finish with the patterns that defeat them and how to spot them.

1A Quick Recap of What the JIT Knows

HotSpot first runs methods in the interpreter and in C1-compiled code that records a profile. The profile says which branches were taken, which concrete classes reached each call, and how often loops repeated. When a method is hot, C2 compiles it again using that profile.

The key idea is speculation. C2 compiles code for what it has actually seen, not for everything that could happen. If only one class ever reached a call, C2 assumes it stays that way and puts a cheap check in the code, called a guard. If the guard ever fails, the JVM throws away the compiled code and goes back to the interpreter, which is called deoptimization, and later recompiles with the new information. Almost every optimisation below depends on this.

2Inlining: The Optimisation That Unlocks the Others

Inlining means copying the body of a called method into the caller, instead of making a call. A call itself costs only a few nanoseconds, but the bigger win is what comes next: once the callee's code sits inside the caller, C2 can optimize them together, folding constants, removing null checks it already did, and seeing that an object never escapes.

Java
record Point(int x, int y) {
int lengthSquared() { return x * x + y * y; }
}
int sum(Point[] points) {
int total = 0;
for (Point p : points) total += p.lengthSquared(); // after inlining: total += p.x * p.x + p.y * p.y
return total;
}

HotSpot inlines small methods freely and larger ones only when they are hot. The limits are set by flags such as MaxInlineSize (about 35 bytes of bytecode for methods that are not hot) and FreqInlineSize (about 325 bytes for hot ones), and inlining also stops at a certain depth. So very large methods are poor candidates, and long chains of tiny wrappers can hit the depth limit before reaching the code that matters. Getters, small records and short helper methods, on the other hand, cost nothing once inlined.

3Devirtualization: Turning Interface Calls Into Direct Calls

In Java, most calls are virtual: when we call list.size(), the JVM must look at the object's class to find which size() to run. A virtual call cannot be inlined until the JIT knows the target. HotSpot finds it in two ways.

Class hierarchy analysis, or CHA, looks at the classes loaded so far. If only one loaded class implements a method, the call has only one possible target, and C2 can call or inline it directly; if a new subclass is loaded later, the dependent code is deoptimized. The second way is the profile. If a call site has seen one class, it is monomorphic, and C2 inlines that class's method behind a type guard. With two classes it is bimorphic, and C2 can inline both behind a type check. With three or more it is megamorphic, and C2 usually falls back to a real virtual call through the method table, with no inlining.

Java
interface Shape { double area(); }
double total(List<Shape> shapes) {
double t = 0;
for (Shape s : shapes) t += s.area(); // call site in the profile
return t;
}
// Only Circle seen -> monomorphic: inline Circle.area() behind one type check
// Circle and Square seen -> bimorphic: inline both behind a type switch
// Circle, Square, Triangle -> megamorphic: virtual call, no inlining, no further optimisation
How the receiver profile changes one call site
Classes seen at the callNameWhat C2 does
1monomorphicinline that class's method behind a type guard
2bimorphicinline both methods behind a type check
3 or moremegamorphicvirtual call through the method table, no inlining
The profile is per call site, so a shared helper called with many types becomes megamorphic for everyone.

4Escape Analysis: Removing Allocations and Locks

Escape analysis asks whether an object can be seen outside the method that created it, after inlining. If it is not stored in a field, not returned and not passed to code the JIT cannot see, the object does not escape. Then C2 can do two things. With scalar replacement, it does not allocate the object at all and keeps its fields in CPU registers. With lock elision, it removes synchronization on the object, because no other thread can ever see it.

Java
long distance(int x1, int y1, int x2, int y2) {
Point d = new Point(x2 - x1, y2 - y1); // never leaves this method
return d.lengthSquared(); // after inlining: no allocation, just (dx*dx + dy*dy)
}
String join(String a, String b) {
StringBuffer sb = new StringBuffer(); // synchronized methods, but sb never escapes
return sb.append(a).append(b).toString(); // the locks can be elided
}

Notice that escape analysis depends on inlining. If lengthSquared() were not inlined, the Point would be passed to a real call, so it would escape, and the allocation would stay. That is why inlining comes first.

5Loop Optimisations

Loops are where most CPU time goes, so C2 works hard on them. Range check elimination removes the array bounds check from each iteration when it can prove the index stays inside the array, for example in a counted loop from 0 to a.length. Loop unrolling repeats the loop body several times per iteration to cut the loop overhead. Vectorization, which HotSpot calls superword, turns simple loops over arrays into SIMD instructions that process 4, 8 or more elements at once.

These work best on simple counted loops over arrays of primitives, with an int index and no calls the JIT cannot inline inside the body. A loop over a List of boxed Integer values, or with a megamorphic call inside it, gets far less of this treatment.

Java
int sum(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) s += a[i]; // bounds checks removed, unrolled, may be vectorized
return s;
}

6The Patterns That Defeat the JIT

Putting it together, a few patterns undo most of the gains. Megamorphic call sites in hot code, such as one generic handler that calls an interface implemented by twenty classes, stop inlining, and with it escape analysis. Huge methods are a problem too: HotSpot does not compile methods larger than 8,000 bytes of bytecode at all by default, so a giant generated method may stay interpreted forever, and large methods are not inlined into their callers. Frequent deoptimization from changing types, for example a call site that saw only one class during warm-up and then meets new ones in production, makes the JIT throw away code and recompile repeatedly.

To find these problems we look rather than guess. -XX:+PrintCompilation shows compilations and made not entrant lines for deoptimized code, -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining shows inlining decisions with reasons such as too big or callee is too large, and JDK Flight Recorder records compilation and deoptimization events; check your version for the exact event names. JMH with its profilers is the right tool to confirm a change actually helps.

7Follow-up questions

Does making methods final help the JIT?

Rarely, in modern HotSpot. Class hierarchy analysis already treats a method with a single loaded implementation as effectively final, and the profile handles the rest. Final can still document intent and matters for some designs, but adding final to methods for speed is a myth from much older JVMs.

Why can a microbenchmark make megamorphic code look fast?

Because the benchmark often calls the code with only one or two types, so the call site stays monomorphic and everything gets inlined. In production the same site sees many types and becomes megamorphic. Good benchmarks feed realistic mixes of types, or run the code with several types during warm-up, to get the profile production will have.

Can escape analysis remove an allocation that is stored in a local collection?

Usually not. If the object is added to an ArrayList, the list's code stores it in an array field, so from the JIT's point of view it escapes into the heap. Escape analysis is most effective on short-lived helper objects such as small records, iterators and builders whose whole lifetime is inside inlined code.

8Conclusion

In this answer, we saw that C2 optimizes speculatively from the profile, that inlining is the most important optimisation because it opens the way for the others, and how devirtualization, escape analysis and loop optimisations build on it. We also saw the patterns that defeat them, megamorphic call sites, huge methods and frequent deoptimization, and the flags and tools that show what the JIT actually did.

What to say in the interviewAbout 80 seconds aloud

Open with

The most important JIT optimisation is inlining, because once a callee's body is inside the caller, C2 can optimize them together. HotSpot inlines small methods freely and larger hot methods up to a limit, so very large methods and deep chains of wrappers limit it.

Then, if they want more

Inlining needs to know the call target, which is where devirtualization comes in: class hierarchy analysis notices when only one loaded class implements a method, and the profile tells C2 which classes actually reached a call site.

With one or two classes, C2 inlines behind a cheap type guard; with three or more, the site is megamorphic and it falls back to a virtual call with no inlining. After inlining, escape analysis can see that an object never leaves the method, so it can replace the allocation with registers and remove locks on it.

For loops, C2 eliminates range checks, unrolls the body and can vectorize simple array loops. The patterns that defeat all this are megamorphic call sites in hot code, huge methods, since anything over 8,000 bytes of bytecode is not compiled by default, and types that change after warm-up, which cause deoptimization and recompilation.

I would confirm any of this with PrintInlining, PrintCompilation or JFR, and measure changes with JMH rather than guessing.

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 "Name the JIT optimisations that matter most for Java performance and the code patterns...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/06521aee8c/