The AOT compilation pipeline
GraalVM Native Image performs ahead-of-time compilation, converting your Java bytecode into a standalone native executable. Unlike the JVM, which interprets bytecode and JIT-compiles hot methods at runtime, native image does all compilation at build time through a process called static analysis.
The pipeline works like this:
- Points-to analysis, Determines which classes, methods, and fields are reachable from the entry point
- Initialization, Runs class static initializers (at build time or deferred to runtime)
- Compilation, Compiles reachable methods to native code using the Graal compiler
- Linking, Produces a standalone executable with the SubstrateVM runtime
# Basic native image build for a Spring Boot app
./mvnw -Pnative native:compile
# Or with Gradle
./gradlew nativeCompile
Spring Boot 3.x includes native image support via Spring AOT (Ahead-of-Time) processing. During build, Spring AOT analyzes your application context and generates:
- Reflection hints for beans and configurations
- Proxy class registrations
- Serialization metadata
- Resource registration patterns
// build.gradle.kts
plugins {
id("org.graalvm.buildtools.native") version "0.10.4"
}
graalvmNative {
binaries {
named("main") {
imageName.set("auth-service")
mainClass.set("com.example.AuthServiceApplication")
buildArgs.add("-O2")
buildArgs.add("-march=native")
buildArgs.add("--gc=G1")
buildArgs.add("-H:+ReportExceptionStackTraces")
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(21))
vendor.set(JvmVendorSpec.GRAAL_VM)
})
}
}
}
The reflection configuration nightmare
Java's reflection API, Class.forName(), Method.invoke(), Field.get(), is everywhere. Spring uses it for dependency injection, Jackson for JSON serialization, Hibernate for entity mapping. Native image's static analysis can't see through reflection, so every reflective access must be declared upfront.
The native-image-agent is your best friend here. It's a JVM agent that records all reflective access, JNI calls, proxy creation, and resource loading during runtime:
# Run your app with the tracing agent
java -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image \
-jar target/auth-service.jar
# Now exercise every code path, run your full test suite
./run-integration-tests.sh
# The agent generates these files:
# reflect-config.json
# jni-config.json
# proxy-config.json
# resource-config.json
# serialization-config.json
The generated reflect-config.json looks like this:
[
{
"name": "com.example.model.User",
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allDeclaredFields": true
},
{
"name": "com.example.config.SecurityConfig$$SpringCGLIB$$0",
"allDeclaredConstructors": true,
"allPublicMethods": true
},
{
"name": "com.fasterxml.jackson.databind.ser.std.ToStringSerializer",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
}
]
The problem: the agent only records code paths that actually execute during tracing. Miss a code path, and a NoSuchMethodException or ClassNotFoundException appears in production. A common example: a token refresh endpoint, which only triggers when tokens are near expiry, throwing a reflection error two weeks after deployment.
A robust approach: run the agent against three different scenarios:
# 1. Full integration test suite
java -agentlib:native-image-agent=config-merge-dir=native-config \
-jar app.jar &
./run-integration-tests.sh
kill %1
# 2. Manual smoke test of less-tested paths
java -agentlib:native-image-agent=config-merge-dir=native-config \
-jar app.jar &
curl -X POST localhost:8080/auth/refresh -H "Authorization: Bearer expired_token"
curl -X POST localhost:8080/auth/logout
curl -X GET localhost:8080/actuator/health
curl -X GET localhost:8080/actuator/prometheus
kill %1
# 3. Error paths, trigger validation errors, 404s, auth failures
java -agentlib:native-image-agent=config-merge-dir=native-config \
-jar app.jar &
curl -X POST localhost:8080/auth/login -d '{"bad":"json"}'
curl -X GET localhost:8080/nonexistent
curl -X POST localhost:8080/auth/login -d '{"email":"x","password":"wrong"}'
kill %1
The config-merge-dir flag is critical, it merges new entries with existing config instead of overwriting.
Build time and CI/CD impact
Here's our multi-stage Dockerfile:
# Stage 1: Build native image
FROM ghcr.io/graalvm/native-image-community:21 AS builder
WORKDIR /app
COPY . .
RUN ./mvnw -Pnative native:compile -DskipTests \
-Dspring.native.build.args="-O2 --gc=G1 -march=x86-64-v3"
# Stage 2: Minimal runtime image
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
libz-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/auth-service /app/auth-service
EXPOSE 8080
ENTRYPOINT ["/app/auth-service"]
The final image is 82MB, compared to 340MB for the JVM-based image with a JDK. In a Kubernetes cluster running 40 instances, that's 10GB less to pull on deployments.
Memory footprint: the hidden complexity
RSS (Resident Set Size) is what matters, not just heap. A native image's memory consists of:
- Image heap, Objects initialized at build time, mapped into memory
- Runtime heap, Objects allocated at runtime (managed by GC)
- Code cache, Compiled native code
- Thread stacks, Default 1MB per thread
- Native memory, JNI allocations, NIO buffers
# Monitor native image memory breakdown
auth-service -XX:+PrintGC -XX:+VerboseGC \
-Xmx128m \
-XX:MaxDirectMemorySize=64m \
-Dserver.tomcat.threads.max=50
The gotcha: image heap is memory-mapped from the executable file, so it shows up in RSS but is shared across processes and doesn't consume physical RAM proportionally. Tools like pmap give you better visibility:
# Detailed memory map
pmap -x $(pgrep auth-service) | tail -5
# Or use NativeMemoryTracking equivalent
auth-service -XX:+DashboardAll -XX:DashboardDump=heap-info.json
Operational considerations
Use it for: CLI tools, serverless functions, short-lived batch jobs, low-traffic microservices, sidecar containers, and any workload where startup time or idle memory matters more than peak throughput.
Avoid it for: high-throughput services where JIT optimization matters, apps with heavy use of dynamic class loading (plugins, OSGI), services that depend on libraries without native image support.
The reflection problem doesn't go away: Even with Spring AOT, third-party libraries surprise you. Budget time for tracing agent runs after every dependency upgrade.
Monitoring: The JMX MBeans you rely on for JVM monitoring don't exist in native image. VisualVM doesn't connect. You need to instrument your application differently, Micrometer with Prometheus works, but Java Flight Recorder doesn't.
Editorial update (November 10, 2026): unsupported experience claims and numerical outcomes were removed. Examples are instructional.