Return to site

☕⚙️ Java 28 JEP 544: WHAT CHANGES INSIDE HOTSPOT with AOT CODE COMPILATION?

Same JAR. Same JVM compilers. Now with reusable native code!

· java

🔸 TL;DR

JEP 544 extends Project Leyden by storing optimized native code in Java's Ahead-of-Time (AOT) cache.

Previously, JEP 483 cached loaded/linked classes, and JEP 515 cached method profiles.

Now, JEP 544 also caches compiled CPU instructions, reducing startup and warmup overhead while preserving JIT flexibility.

Let's explore the architecture in 4️⃣ BEFORE vs AFTER changes!

Section image

🔸 1️⃣ AOT CACHE: FROM METADATA TO NATIVE CODE

▪️ BEFORE: The AOT cache stored loaded/linked classes and method profiling data.

▪️ AFTER: It additionally stores optimized native machine code for selected methods.

What is native code?

CPU instructions generated from Java bytecode by HotSpot's compilers, executable directly by an x64 or AArch64 processor.

Unlike .class bytecode, these instructions no longer need interpretation or JIT compilation before execution.

Architecture:

BEFORE:
app.aot
├── Loaded/linked classes
└── Method profiles

AFTER:
app.aot
├── Loaded/linked classes
├── Method profiles
└── Native CPU instructions ← NEW

💡 This is not a standalone native executable like GraalVM Native Image. HotSpot still manages execution.

#AOTCache

🔸 2️⃣ C1/C2: FROM RUNTIME TO AOT COMPILATION

▪️ BEFORE: C1/C2 compiled hot methods into native code during application execution. This code was lost when the JVM stopped.

▪️ AFTER: The existing C1/C2 compilers can also generate native code during AOT cache creation.

What do C1/C2 actually do?

▪️ C1: Quickly translates Java bytecode into machine instructions with lightweight optimizations.

▪️ C2: Performs advanced optimizations such as method inlining, type specialization and dead-code elimination.

During AOT creation, HotSpot uses profiles captured during a training run to select and optimize methods, then saves their compiled code.

Architecture:

BEFORE:
[Bytecode]
    ↓
[Runtime C1/C2]
    ↓
[Native code in JVM memory]

AFTER:
[Training run] → [Profiles]
                       ↓
              [AOT cache creation]
                       ↓
                    [C1/C2]
                       ↓
             [Native code in app.aot]

💡 No new compiler! Existing HotSpot compilation technology is reused ahead of production execution.

#JITCompiler

🔸 3️⃣ HOTSPOT: FROM JIT WARMUP TO CODE REUSE

▪️ BEFORE: HotSpot interpreted bytecode and progressively compiled hot methods through JIT during startup and warmup. Cached profiles could accelerate this process.

▪️ AFTER: HotSpot can immediately reuse compatible native code from the AOT cache, avoiding repeated compilation for those methods.

What happens when the workload changes?

HotSpot can still deoptimize cached code and invoke C1/C2 to generate new optimized instructions.

Architecture:

[Startup] → [Interpreter]
                 ↓
            [C1/C2 JIT]
                 ↓
         [Peak performance]

AFTER:
[Startup] → [AOT native code]
                 ↓
          [Workload changes?]
             ↓ YES
           [C1/C2 JIT]
                 ↓
        [Reoptimized code]

💡 AOT accelerates execution; JIT preserves runtime adaptability.

#HotSpot

🔸 4️⃣ DEPLOYMENT: SAME JAR, EXTERNAL AOT CACHE

▪️ BEFORE: A Java application ran from its JAR, optionally using an AOT cache containing classes and profiles.

▪️ AFTER: The JAR remains unchanged, while the external AOT cache can additionally contain native machine code.

Where is app.aot stored?

It is a separate binary file, NOT embedded inside the JAR.

Architecture:

BEFORE:
[Container image]
├── app.jar
└── app.aot (optional)

AFTER:
[Container image]
├── app.jar       ← Unchanged
└── app.aot       ← + Native code

Generate the cache:

java -XX:AOTCacheOutput=app.aot -jar app.jar

Run in production:

java -XX:AOTCache=app.aot -jar app.jar

The relative path creates or locates app.aot in the current working directory. It can be packaged alongside the JAR inside a Docker image.

⚠️ Reusing AOT native code requires compatible CPU features and the same garbage collector between training and production.

💡 No new container format, no bytecode changes, no application rewrite!

#JavaDeployment

🔸 TAKEAWAYS 🎯

▪️ AOT CACHE: Now stores native CPU instructions alongside classes and profiles.

▪️ C1/C2: Existing compilers additionally generate reusable code during AOT cache creation.

▪️ HOTSPOT: Executes cached optimized code sooner, with JIT available for changing workloads.

▪️ DEPLOYMENT: Same JAR, external .aot file containing the new compiled code.

▪️ PERFORMANCE: JEP 544 benchmarks report 65 to 80% shorter startup times with AOT code enabled on five framework applications, compared with no AOT cache.

The innovation is not a new compiler or a new Java binary. It is moving compilation work out of production startup and reusing its results across JVM executions!

💬 Would you add an AOT cache to your Java microservices deployment?

🔗 Official JEP 544: https://openjdk.org/jeps/544

#Java #Java28 #JEP544 #ProjectLeyden #JVM #AOT #JIT #HotSpot #JavaPerformance #OpenJDK #JavaArchitecture #Microservices #Docker #SoftwareEngineering

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇