Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

A Java service is slow for its first minutes after each deployment and then becomes fast without any change in load. What happens inside HotSpot during that time, and what does it mean for deployments and benchmarks?

For its first minutes a HotSpot JVM runs most of our code in the interpreter while it collects profiles, loads and initializes classes on first use, and compiles the busiest methods first with the quick C1 compiler and later with the optimizing C2 compiler. That warm-up costs time and CPU, so deployments should warm instances up before giving them full traffic, and benchmarks must throw away warm-up iterations, which is what JMH does for us.

Why this question is asked

Many teams notice that a Java service is slow just after a deployment and fast a few minutes later, even though nothing about the traffic changed. It shows up as a latency spike on every release, as timeouts from callers, and as benchmarks that give different answers each time they run. Interviewers ask about it because it shows whether we know how the JVM actually executes our code. In this answer, we will recap how HotSpot runs bytecode, follow a method from its first call to fully optimized machine code, add the cost of class loading, and then see what this means for deployments and for benchmarks.

1A Quick Recap of How HotSpot Runs Our Code

Our Java code is compiled by javac into bytecode, a compact set of instructions for an imaginary machine. The real CPU cannot run bytecode, so the JVM has to turn it into actions somehow. HotSpot, the JVM inside almost every OpenJDK build, has two ways to do it.

The first is the interpreter. It reads one bytecode instruction at a time and performs it. It can start running immediately, but it is slow, often ten or more times slower than compiled code. The second is the JIT compiler, short for just-in-time compiler. It translates a whole method into native machine code while the program runs. That code is fast, but compiling takes time and CPU, so the JVM only does it for methods that are used a lot.

2Following One Method From Cold to Hot

HotSpot uses tiered compilation, which means a method moves through several stages as it gets busier. At first the method runs in the interpreter, and the interpreter keeps counters of how often the method is called and how often its loops repeat.

When those counters pass a threshold, the method is compiled by C1, the client compiler. C1 compiles quickly and produces decent code, and the version it produces also records a profile: which branches are taken, which concrete classes reach each call, and how long loops run. When the method keeps being busy, its counters pass a second, higher threshold, and C2, the server compiler, recompiles it. C2 is slow to compile, but it uses the profile to make aggressive bets, such as inlining the one class that has ever reached a call site, and produces the fastest code.

Java
// Run with: java -XX:+PrintCompilation -jar app.jar (one line per compilation)
// timestamp id tier method
// 212 88 3 com.acme.PriceService::quote (64 bytes) <- C1 with profiling
// 4810 912 4 com.acme.PriceService::quote (64 bytes) <- C2, optimized
// 4811 88 3 com.acme.PriceService::quote (64 bytes) made not entrant <- old C1 version retired
Step throughA method's path through tiered compilation
  1. First calls run in the interpretercounts calls and loop iterations
  2. Busy enough for C1?
    YesCompile quickly with profiling (tier 3)NoStay interpreted
  3. Profile collected under real trafficbranches taken, receiver classes, loop counts
  4. Still hot?
    YesC2 recompiles with the profile (tier 4)NoKeep the C1 code
  5. Profile assumption broken?a new class or branch appears
    YesDeoptimize and go back to interpretingNoFast code keeps running

5 steps in order. Play them, or step through them one at a time.

Most requests during warm-up run on code from the first two steps.

Notice the gap between the two lines for the same method: almost five seconds in this example, and in a large service with thousands of hot methods the whole process takes minutes. The reason is that C2 can only optimize well once it has seen enough real traffic to trust the profile. So the early requests run on interpreted or C1 code, and the compiler threads compete with request threads for CPU while they work.

3Class Loading Adds to the Early Slowness

The JIT is not the only thing happening in those first minutes. The JVM loads classes lazily, the first time something uses them. Loading a class means finding its bytes in a JAR, parsing them, verifying the bytecode is safe, and running its static initializer. A Spring Boot service can load 15,000 or more classes, and many of them are touched for the first time by the first request on each code path: the first JSON response loads the Jackson serializers for that type, the first database call loads the driver's classes, the first TLS connection initializes the crypto providers.

So the very first request on each path pays for class loading and initialization, and the next few thousand pay for interpreted code. That is why the slowness fades gradually instead of disappearing at once.

4What It Means for Deployments

If a load balancer sends a new instance its full share of traffic the moment the process starts, every user who lands there in the first minutes sees the slow code, and the compiler threads have less CPU to finish their work. The fix is to let the instance warm up before it counts as ready.

One way is to run a warm-up step before readiness: the service calls its own main endpoints a few thousand times with realistic requests, and only then reports ready, for example by changing Spring Boot's readiness state so the Kubernetes readiness probe starts passing. Another way is a gradual traffic shift, where the load balancer or service mesh ramps a new instance from a small share up to its full share over a few minutes. A third factor is CPU: if a container is limited to one CPU, the JIT threads and the request threads fight for it, and warm-up takes much longer, so tight CPU limits during startup make the problem worse.

5What It Means for Benchmarks

The same effect ruins naive benchmarks. If we time the first thousand calls of a method, we mostly measure the interpreter and the compilers, not the code we care about. The numbers also change between runs, because the JIT may make different choices each time.

This is why JMH, the Java Microbenchmark Harness from the OpenJDK project, separates warm-up iterations from measurement iterations. It runs the code for a set time without recording results, then measures, and it repeats the whole thing in several fresh JVM forks so one lucky or unlucky compilation does not decide the result. For whole-service load tests the same rule applies: run a warm-up phase, then measure, and never compare a cold run with a warm one.

Java
@Warmup(iterations = 5, time = 1) // 5 seconds of unmeasured runs to let C1 and C2 finish
@Measurement(iterations = 5, time = 1) // then 5 measured seconds
@Fork(3) // in 3 fresh JVMs
@State(Scope.Benchmark)
public class QuoteBench {
PriceService service = new PriceService();
@Benchmark public Quote quote() { return service.quote("ACME", 100); }
}

6Follow-up questions

What can reduce warm-up time, for example CDS or the AOT cache in recent JDKs?

Class Data Sharing, or CDS, stores already parsed class data in an archive file that the JVM maps into memory at startup, so classes load faster; the JDK ships a default archive for its own classes, and AppCDS extends it to application classes, for example with -XX:ArchiveClassesAtExit. JDK 24 added an ahead-of-time cache as part of Project Leyden (JEP 483) that also stores classes already loaded and linked from a training run, and JDK 25 made it easier to create and added stored method profiles so the JIT can start optimizing sooner; check your version for the exact options. Outside HotSpot, CRaC in some distributions restores a snapshot of a warmed-up process, and GraalVM Native Image compiles everything ahead of time.

Why not just make the JIT compile everything at startup?

Because the best optimizations depend on the profile. C2 inlines and specializes code based on which classes and branches it actually saw, and compiling everything blindly at startup would take a long time, use a lot of CPU and memory, and produce worse code. Tiered compilation is a deliberate trade: start quickly, and spend compiler effort only where real traffic shows it pays off.

Does warm-up start again after a garbage collection or a traffic spike?

No. Compiled code stays in the code cache for the life of the process. A deoptimization can send individual methods back to the interpreter when an assumption breaks, for example when a new class reaches a call site, but that is local and short. Warm-up as a whole happens once per JVM start, which is why it shows up after deployments and restarts.

7Conclusion

In this answer, we saw that a fresh JVM runs our code in the interpreter while collecting profiles, compiles busy methods with C1 and the hottest with C2, and loads and initializes classes on first use, all of which makes the first minutes slow. We saw that deployments should warm instances before giving them full traffic, and that benchmarks must separate warm-up from measurement, as JMH does.

What to say in the interviewAbout 90 seconds aloud

Open with

What we are seeing is JVM warm-up. HotSpot starts by running our bytecode in the interpreter, which is slow but starts immediately, and it counts how often each method and loop runs.

Then, if they want more

Methods that get busy are compiled by C1, which is fast to compile and also records a profile of branches and receiver types. The hottest methods are later recompiled by C2, which uses that profile to inline and specialize aggressively, and that is where the real speed comes from.

On a large service that takes minutes, and the compiler threads use CPU the request threads also need. On top of that, classes are loaded, verified and initialized the first time something uses them, so the first request on each path also pays for loading serializers, drivers and crypto.

For deployments, this means a new instance should not take full traffic immediately. I would either run a warm-up step that exercises the main endpoints before the readiness probe passes, or ramp traffic gradually, and make sure the container has enough CPU during startup.

For benchmarks, it means we must discard warm-up and measure in several fresh JVMs, which is exactly what JMH does with its warm-up iterations and forks. To shorten warm-up itself, I would look at CDS and AppCDS, and on JDK 24 and later the Leyden ahead-of-time cache.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Java 25
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 "A Java service is slow for its first minutes after each deployment and then becomes fast...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/ba3bb2abe5/