JVM mode vs native image
- JVM
- Java Virtual Machine
Mid-level Always asked 7 min
On this page
The question
What is a GraalVM native image, and how does Quarkus build one?
30-second answer
A native image is a standalone executable that the native-image tool compiles ahead of time from the app and every library it reaches. It has no JIT (just-in-time) compiler, loads no new classes, and starts from a heap snapshot made at build. Quarkus builds one with ./mvnw package -Dnative, and can run the build inside a container with Mandrel, so GraalVM need not be installed.
Understand it
JVM mode or native image?
Explore4 busy replicas that rarely restart: startup hardly matters, and the JIT reaches higher peak throughput. Native would save only 400 MB.
View as text
Four pairs of bars compare JVM mode and a native image for a typical small REST service, not a benchmark: startup, about 1 s against 0.05 s; memory for all replicas, 150 MB against 50 MB each; peak throughput, higher on the JVM; and build time, about 20 s against 3 minutes. A mark gives the verdict.
1 scale-to-zero replica: cold starts take about 0.05 s instead of 1 s, and memory totals 50 MB instead of 150 MB. Builds take minutes.
10 scale-to-zero replicas: cold starts take about 0.05 s instead of 1 s, and memory totals 500 MB instead of 1.5 GB. Builds take minutes.
2 busy replicas that rarely restart: startup hardly matters, and the JIT reaches higher peak throughput. Native would save only 200 MB.
20 busy replicas: native saves 2.0 GB of memory, and the JVM keeps higher peak throughput. Measure both under real load.
Four pairs of bars compare JVM mode and a native image for a typical small REST service, not a benchmark: startup, about 1 s against 0.05 s; memory for all replicas, 150 MB against 50 MB each; peak throughput, higher on the JVM; and build time, about 20 s against 3 minutes. A mark gives the verdict.
1 scale-to-zero replica: cold starts take about 0.05 s instead of 1 s, and memory totals 50 MB instead of 150 MB. Builds take minutes.
10 scale-to-zero replicas: cold starts take about 0.05 s instead of 1 s, and memory totals 500 MB instead of 1.5 GB. Builds take minutes.
2 busy replicas that rarely restart: startup hardly matters, and the JIT reaches higher peak throughput. Native would save only 200 MB.
20 busy replicas: native saves 2.0 GB of memory, and the JVM keeps higher peak throughput. Measure both under real load.
Takeaway: Native images start in milliseconds and use a fraction of the memory, but build for minutes and peak lower. Choose by how often instances start and how many run.
The closed-world assumption
Tap the partsSelect a part to see what it does.
Tap a rule of the closed world. Each row shows what the build must know, and what happens to code that breaks the rule.
View as text
A banner reads: known at build, or not in the binary. Below it, seven rows, one per rule, each with a button and what the rule means; rules that break code that works on the JVM are marked.
Reachability: only reachable code is compiled in. native-image starts from main and follows every call it can prove.
Reflection: register it at build or it is missing. A class used only through reflection looks unreachable, so its fields and methods are left out.
Class loading: no new classes after the build. Class.forName finds only classes included at build.
Proxies: interfaces known at build time. A JDK (Java Development Kit) dynamic proxy works only for interface lists known at build.
Resources: included only when listed. A file read with getResource must be in the image.
Static init: can run at build stored in the image. Classes can be initialized at build, with their objects stored in the image heap, so they are ready at startup.
No JIT: machine code ahead lower peak throughput. Everything is compiled at build, so there is no warm-up.
A banner reads: known at build, or not in the binary. Below it, seven rows, one per rule, each with a button and what the rule means; rules that break code that works on the JVM are marked.
Reachability: only reachable code is compiled in. native-image starts from main and follows every call it can prove.
Reflection: register it at build or it is missing. A class used only through reflection looks unreachable, so its fields and methods are left out.
Class loading: no new classes after the build. Class.forName finds only classes included at build.
Proxies: interfaces known at build time. A JDK (Java Development Kit) dynamic proxy works only for interface lists known at build.
Resources: included only when listed. A file read with getResource must be in the image.
Static init: can run at build stored in the image. Classes can be initialized at build, with their objects stored in the image heap, so they are ready at startup.
No JIT: machine code ahead lower peak throughput. Everything is compiled at build, so there is no warm-up.
Takeaway: A native image contains only what the build can prove is used. Anything found at run time, such as reflection, resources or new classes, must be declared ahead.
In code
@RegisterForReflection
public class LegacyReport {
public String title;
public int pages;
}
class ReportLoader {
Object load(String className) throws Exception {
// Works in a native image only for classes registered at build
return Class.forName(className).getConstructor().newInstance();
}
}@QuarkusIntegrationTest
class OrderResourceIT extends OrderResourceTest {
}quarkus.native.resources.includes=reports/*.json./mvnw verify -Dnative -Dquarkus.native.container-build=true
./target/orders-1.0.0-runner -Xmx64mSolve it
The native build passes, then fails on the first real request
The JVM build works. The native executable starts in 30 ms, but the first call that parses a vendor’s JSON fails: Jackson
cannot create com.vendor.RateDto. Nothing in your code calls that class directly. Explain and fix it.
- Reproduce in a test. Run the HTTP tests against the binary with
@QuarkusIntegrationTest, so CI catches it, not production. - Recall the closed-world rule. The native build keeps only code it can prove is reachable. A class reached only through reflection, such as by a JSON mapper, is left out or has no reflection data.
- Find the class. The DTO (data transfer object) comes from a vendor library, so you cannot add an annotation to it.
- Register it.
@RegisterForReflection(targets = ...)on any class of yours registers classes you do not own. Resources read by name needquarkus.native.resources.includes. - Check. The native integration test passes, and it runs in CI on every change to the vendor library.
@RegisterForReflection(targets = { RateDto.class, RateItem.class }) // vendor classes, read by Jackson
public class VendorReflectionConfig {}
@QuarkusIntegrationTest // runs against the native executable
class RatesResourceIT extends RatesResourceTest {}How to answer
- It compiles under a closed-world assumption, so only code reachable at build is included.
- Mandrel is a GraalVM distribution from Red Hat, focused on native images for Quarkus.
- The binary is platform-specific, so build it for the operating system of the container.
@QuarkusIntegrationTestruns tests against the built binary.
Common mistakes
- Calling a native image a faster-starting JVM. It has no JIT and runs on a small runtime called SubstrateVM.
- Building native for every local change. Develop in JVM mode, and build native in CI (continuous integration).
Follow-up questions
Where do the startup and memory gains of a native image come from?
Nothing loads or verifies classes at startup, because the code is already machine code. No JIT compiler runs, so there is no warm-up and no memory for compiled code or profiles. Objects initialized during the build are stored in the image heap and are ready at once. Quarkus adds to this, since its build-time processing leaves little framework work for startup.
- For a small service, startup typically falls from about a second to tens of milliseconds.
- Memory falls because no class metadata, JIT compiler or profiling data is kept.
- Measure RSS (resident set size), the memory the process really uses, not only the heap.
What do you give up with a native image?
Build time, often minutes and several gigabytes of memory instead of seconds. Peak throughput, because no JIT optimizes the code from run-time profiles, so a warmed-up JVM is usually faster on long runs. Dynamic features, since reflection, resources and proxies must be known at build. Tooling, because Java agents and many JVM monitoring tools do not work the same way.
- Profile-guided optimization in Oracle GraalVM narrows the throughput gap.
- A library that relies on reflection may need configuration or a Quarkus extension.
- Some failures appear only in the binary, so test the binary itself.
In the native binary only, a class read through reflection has no fields. Why, and how do you fix it?
The native build keeps only what it can see being used. A class reached only through reflection looks unused, so its fields and methods are left out, and reflection finds nothing at run time. Quarkus registers the types its extensions use, such as REST (Representational State Transfer) payloads. For other classes, add @RegisterForReflection, or a reflection configuration file.
- The GraalVM tracing agent can record the reflection a test run uses.
- Prefer an extension that registers what it needs over hand-written configuration.
- Run integration tests against the native binary in CI to catch this early.
Which workloads benefit most from a native image?
Those that start often or run many small instances: serverless functions, services that scale to zero, command-line tools, and fleets of small pods where memory sets the cost. Long-running services under heavy, steady load usually do better on the JVM, where the JIT reaches higher peak throughput and builds are fast.
- Cold start time decides whether scaling to zero is acceptable.
- Memory per instance decides how many instances fit on a node.
- Decide per service, since one code base can build both.
How do you test a native build in Quarkus?
Build the binary, then run tests annotated with @QuarkusIntegrationTest. They start the built artifact as a separate process and call it over HTTP, so they cannot inject beans, but they check the real binary, including anything the native build left out. Run them in CI with ./mvnw verify -Dnative, since a native build is too slow for every change.
@QuarkusTestruns in JVM mode with injection;@QuarkusIntegrationTestruns against the package.- An integration test class often extends the
@QuarkusTestclass to reuse its HTTP tests. - The same annotation also tests a fast-jar or a container image.
Why build the native image inside a container?
The binary targets the operating system it was built on. With -Dquarkus.native.container-build=true, the build runs in a Linux container and produces a Linux executable, even from Windows or macOS, without installing GraalVM, and with the same toolchain as CI.
Check yourself
Show answers
- A native image: Correct. It starts in tens of milliseconds, with no class loading or JIT warm-up.
- JVM mode: Incorrect. A JVM start of about a second leaves no room for the first request.
- Either, they start equally fast: Incorrect. Startup is exactly where they differ the most.
Show answers
- The build left out what it could not see being used.: Correct. Reflection hides the use from the analysis, so the class must be registered.
- The JIT compiler removed the fields.: Incorrect. A native image has no JIT compiler at run time.
- The garbage collector freed the class.: Incorrect. Classes are not collected away like this; they were never included.
Show answers
- JVM mode, once the JIT has warmed up: Correct. The JIT optimizes from what the code really does, which ahead-of-time compilation cannot see.
- The native image, since it is precompiled: Incorrect. It starts fast, but without run-time profiles its peak is usually lower.
- They always reach the same throughput: Incorrect. The compilers differ, so their peaks differ too.