Ahead-of-time startup (Project Leyden)

Java 25 Course · lesson 7 of 16 · 5 min read

The JVM starts remembering what it learned last time, instead of relearning it every boot.

Open this lesson in the learning hub

Key points

  • Java is slow to start because the JIT must observe your code running before it can optimise it.
  • For a short-lived container or a serverless function, the JVM may never reach peak speed at all.
  • Java 25 lands the first Leyden pieces: an AOT cache of loaded classes and recorded method profiles.
  • You do a training run, the JVM records what it learned, and later runs start from that state.
  • JEP 514 simplified the command line and JEP 515 added profile recording, so warm-up starts ahead.

Example

public class Main {
    public static void main(String[] args) {
        // Watch the JIT warm up: the same work gets faster as the JVM observes it.
        for (int round = 1; round <= 5; round++) {
            long t = System.nanoTime();
            long sink = 0;
            for (int i = 0; i < 3_000_000; i++) {
                sink += hash(i);
            }
            long us = (System.nanoTime() - t) / 1000;
            System.out.println("round " + round + " : " + us + " us   (checksum " + (sink % 97) + ")");
        }
        System.out.println();
        System.out.println("Round 1 is interpreted; later rounds run JIT-compiled code.");
        System.out.println("AOT caching aims to give round-1 something closer to round-5 speed.");
    }

    static int hash(int n) {
        int h = n;
        h ^= (h >>> 16);
        h *= 0x7feb352d;
        h ^= (h >>> 15);
        return h;
    }
}

Leyden attacks the one thing the JIT could never fix: everything it learns dies with the process.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Java 25 Course course, and every lesson in it is listed on the Java 25 Course contents page.