GraalVM and Native Image
GraalVM compiles Java ahead-of-time (AOT) into a native executable — no JVM needed at runtime. The result: instant startup (milliseconds instead of seconds) and much lower memory usage.
What Is GraalVM?
GraalVM is a high-performance JDK distribution that includes:
| Component | Description |
|---|---|
| GraalVM JDK | Drop-in replacement for HotSpot JDK |
| Graal JIT Compiler | Advanced JIT (replaces C2), better peak performance |
| Native Image | AOT compilation to standalone executables |
| Polyglot | Run JS, Python, Ruby, R alongside Java |
JIT vs AOT Compilation
Traditional (JIT — HotSpot):
┌──────────┐ ┌─────────┐ ┌───────────┐
│ .class │ ──► │ JVM │ ──► │ Interpret │
│ bytecode │ │ startup │ │ + JIT │
└──────────┘ └─────────┘ │ optimize │
└───────────┘
↑ Slow startup ↑ Great peak performance
Native Image (AOT — GraalVM):
┌──────────┐ ┌───────────┐ ┌────────────┐
│ .class │ ──► │ native- │ ──► │ Standalone │
│ bytecode │ │ image │ │ executable │
└──────────┘ │ (build) │ │ (no JVM) │
└───────────┘ └────────────┘
↑ Slow build ↑ Instant startup
| Metric | JIT (HotSpot) | AOT (Native Image) |
|---|---|---|
| Startup | 1–10 seconds | 10–100 milliseconds |
| Memory (RSS) | 200–500MB | 30–100MB |
| Peak throughput | Excellent (JIT optimizes) | Good (no runtime optimization) |
| Build time | Fast (seconds) | Slow (minutes) |
| Executable size | JDK required (~200MB) | Self-contained (~50–100MB) |
Installing GraalVM
# Using SDKMAN (recommended)
sdk install java 21.0.2-graal
sdk use java 21.0.2-graal
# Verify
java -version
# openjdk version "21.0.2" ... GraalVM CE
native-image --version
Building a Native Image
Simple Java App
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Native Image!");
}
}
javac Hello.java
native-image Hello -o hello
./hello # prints instantly — no JVM
time ./hello
# real 0m0.008s ← 8 milliseconds!
Spring Boot Native Image
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
# Build native image (takes 3–10 minutes)
mvn -Pnative native:compile
# Run
./target/my-app
# Startup comparison:
# JVM: Started in 2.3 seconds
# Native: Started in 0.045 seconds ← 50× faster
Limitations — The “Closed World” Assumption
Native image analyzes your code at build time — it must see everything. These features are problematic:
| Feature | Problem | Solution |
|---|---|---|
| Reflection | Classes not known at build time | Register in reflect-config.json |
| Dynamic proxies | Created at runtime | Register in proxy-config.json |
| Serialization | Uses reflection internally | Register serializable classes |
| JNI | Native calls not traceable | Register in jni-config.json |
| Resource loading | getResource() not traced |
Register in resource-config.json |
Auto-Configuration with Tracing Agent
# Run your app with the tracing agent — it records all reflection/proxy usage
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image \
-jar app.jar
# Exercise all code paths (run tests, hit all endpoints)
# Agent generates config files automatically
Spring Boot AOT
Spring Boot 3+ includes AOT processing that handles most of this automatically:
mvn -Pnative native:compile
# Spring AOT generates reflection hints at build time
When to Use Native Image
| ✅ Great For | ❌ Not Ideal For |
|---|---|
| Serverless (AWS Lambda, Azure Functions) | Long-running throughput-critical services |
| CLI tools | Apps with heavy reflection (some Spring features) |
| Microservices (fast scale-up) | Apps needing peak JIT-optimized performance |
| Containers (small image size) | Rapid development iteration (slow build) |
| Edge / IoT (low memory) | Large monoliths with many dynamic features |
GraalVM vs Standard JDK — Performance
| Scenario | Winner | Why |
|---|---|---|
| Startup time | Native Image (50×) | No JVM bootstrap, no JIT warmup |
| Memory (cold) | Native Image (3–5×) | No JVM overhead, no JIT compiler in memory |
| Peak throughput | HotSpot JIT | JIT optimizes based on runtime profiling |
| Warmup time | Native Image | No warmup needed |
| Build time | HotSpot | native-image takes minutes |
Rule of thumb: If your app runs for seconds (serverless, CLI), use native image. If it runs for hours/days (servers), HotSpot JIT may give better peak throughput.
Docker with Native Image
# Multi-stage build
FROM ghcr.io/graalvm/native-image-community:21 AS build
WORKDIR /app
COPY . .
RUN mvn -Pnative native:compile
FROM gcr.io/distroless/base-debian12
COPY --from=build /app/target/my-app /app
EXPOSE 8080
ENTRYPOINT ["/app"]
docker build -t my-app-native .
docker images my-app-native
# REPOSITORY SIZE
# my-app-native 85MB ← vs 300MB+ with JDK
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
| Not testing native image | Works on JVM ≠ works as native | Run integration tests against native binary |
| Skipping tracing agent | Missing reflection config → runtime errors | Run agent with full test coverage |
| Expecting JIT-level throughput | Native image has no runtime optimization | Accept tradeoff or stay on HotSpot |
| Using native image for long-running servers | Missing out on JIT benefits | Evaluate if startup matters for your use case |
Key Takeaways
- GraalVM Native Image = AOT compilation → instant startup, low memory.
- Spring Boot 3+ has built-in native image support — one command to build.
- Limitations: reflection, proxies, serialization need explicit registration.
- Serverless, CLI, containers = great fit. Long-running servers = evaluate carefully.
- Use the tracing agent to auto-generate configuration.
- Docker images drop from 300MB+ to ~85MB.
Module Complete
You’ve completed the JVM Internals & Performance module! You now understand:
- JVM architecture, class loading, and memory areas
- Garbage collection algorithms and how to choose
- GC tuning with real flags and log analysis
- Memory leak detection with heap dumps and Eclipse MAT
- Profiling with JFR, async-profiler, flame graphs, and JMH
- GraalVM native image for cloud-native Java