The BackendDeeper

Kayavak / The Backend / BE·40 JVM Internals and Other Runtimes / Deeper

Deeper · The Backend

JVM Internals and Other Runtimes

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Early JVMs interpreted bytecode, and Java earned a reputation for being slow. Research on Smalltalk and Self (David Ungar, Craig Chambers, Urs Hölzle and others, late 1980s and 1990s) had shown how a dynamic runtime could beat static compilers: watch the program run, compile the hot paths with assumptions it observed, and undo them if they break. Several of those researchers founded Animorphic Systems, which Sun bought in 1997; their work became HotSpot, the JVM shipped from 1999 and still the base of OpenJDK.

Garbage collection followed a similar path, from long stop-the-world pauses to collectors that do most work concurrently: G1 (default since Java 9), ZGC (production-ready in Java 15, generational in Java 21) and Shenandoah, with pauses typically under a millisecond. Other runtimes took different points on the spectrum: Go compiles ahead of time with a concurrent collector, .NET has its own JIT, and GraalVM can compile Java ahead of time into a native image.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

A JIT bets on what it has seen. It compiles hot code for the common case, guards the bet, and falls back (deoptimises) when the bet loses. Code that's predictable gets fast; code that keeps changing shape doesn't.

Question Answer
What is it? The machinery that loads, verifies, interprets, compiles and garbage-collects Java programs
Problem it solves Portable bytecode running at native speed with automatic memory management
Parts Class loading and verification, interpreter, tiered JIT (C1, C2), inline caches, deoptimisation, safepoints, garbage collectors
How it works Interpret and profile; compile hot methods with C1, then C2 using the profile; inline and specialise behind guards
Assumptions Recent behaviour predicts future behaviour; most objects die young
When they change Call sites that see many types (megamorphic) can't be inlined; long-lived caches strain generational GC
Where it fails Warm-up time and memory for compiled code and profiles; start-up for short-lived processes
Related ideas Compilers and GC (Foundations, ch10); performance measurement (ch41)

What if?

What if every call site in a hot loop saw ten different implementing classes?

Reason it through, then open

The JIT couldn't inline the call: it inlines when a site sees one class (monomorphic) or two (bimorphic), behind a cheap type check. With many classes it falls back to a virtual dispatch each time, and loses the optimisations inlining enables (escape analysis, constant folding across the call). Grouping work by type, or using sealed types and switch, can turn a megamorphic site back into predictable ones.

What if a server process lived only a second?

Reason it through, then open

It would spend its whole life loading classes and interpreting, never reaching C2-compiled code, so the JIT's peak speed would never matter. That's the serverless and command-line case, and why AOT compilation (GraalVM native image) and start-up caching (Project Leyden, class data sharing) exist.

Where the boundary is

Aspect JIT (HotSpot) AOT (native image)
Start-up Slow until warm Fast
Peak throughput High; uses run-time profiles Usually lower, without profile-guided builds
Memory Higher (compiler, profiles) Lower
Dynamic features (reflection, class loading) Fully supported Must be declared at build time

How sure are we?

Claim How it's known Where from
HotSpot came from Animorphic, acquired by Sun in 1997 Historical Accounts by its developers
Inline caches and deoptimisation came from Self research Historical Hölzle's and Chambers's publications
ZGC pauses are typically under a millisecond Measured OpenJDK reports; depends on workload and version
Megamorphic calls block inlining Convention HotSpot's design; other JITs set different limits

Still open

How to get fast start-up without giving up the JIT's peak performance (Leyden's AOT cache, CRaC checkpoints, native images) is the JVM's main open engineering question.

What would change your mind? If AOT-compiled Java matched JIT peak throughput on long-running servers, the JIT's complexity would be harder to justify. Profile-guided native images narrow the gap.

Recall

What does deoptimisation do?

Discards compiled code whose assumption failed and continues in the interpreter at the same point.

Why are megamorphic call sites slow?

The JIT can't inline them, so each call is a virtual dispatch and later optimisations are lost.

When does AOT compilation beat a JIT?

For short-lived processes and fast start-up, where the JIT never warms up.

Where did HotSpot's techniques come from?

Research on Smalltalk and Self, via Animorphic Systems.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.