Platformsenior8+ years

A JVM microbenchmark that calls a method once and times it with `System.nanoTime()` reports the method as slow; the same method is fast once the service has been handling real traffic for a minute. What's actually different between those two measurements, and why does HotSpot not just compile everything to machine code immediately at startup?

The microbenchmark is measuring the interpreter — the JVM's default way of running bytecode, which re-decodes each instruction every time it runs and is genuinely slower than compiled machine code. After a minute of real traffic, the methods that actually run often have been noticed and compiled to native machine code by the JIT, which is why they're fast by then. HotSpot doesn't compile everything to native code immediately because compiling takes real time and most methods in a typical program run only a handful of times ever — spending compilation effort on code that will barely run would make every program's startup dramatically slower for a benefit the bulk of the code never earns back. So it interprets first, counts how often each method actually gets called, and only compiles the ones that cross a threshold — literally finding the 'hot spots,' which is where the name comes from.

The lesson behind it →
More on Platform