Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain what GraalVM native image does at build time and why it makes Java applications start faster and use less memory.

At build time, GraalVM's native-image tool starts from the application's entry point, works out by static analysis which classes, methods and fields can ever be reached, and compiles only that code ahead of time into a single platform executable. It assumes a closed world, so anything not found reachable is left out, and it can run some class initialisation at build time and store the resulting objects in an image heap. Because there is no class loading, no bytecode interpretation and no JIT warmup at startup, the program starts in milliseconds and uses much less memory, but reflection, proxies, resources and serialization need metadata.

Why this question is asked

Native images changed how people think about Java in containers and serverless platforms, where startup time and memory per instance turn directly into cost and latency. Interviewers ask this question to check that we understand what is actually different about a native executable, not just that it is fast, because every limitation of native images follows from how the build works. In this answer, we will recap how the JVM normally starts a program, follow what native-image does during the build, see why the result starts faster and uses less memory, and understand why dynamic features need extra metadata.

1A Quick Recap of How a JVM Starts a Program

When we run java -jar app.jar, the JVM starts empty. It loads classes from the jar on demand, verifies their bytecode and links them. At first it interprets the bytecode, one instruction at a time. As methods are called often, the JIT compiler, the just-in-time compiler inside the JVM, compiles them to optimised machine code using profiles collected while the program runs. A Spring Boot application loads many thousands of classes this way, and it takes a while, often a minute or more under load, before the hot code is fully compiled.

All of this machinery lives in memory: class metadata, the JIT compiler and its profiles, the compiled code cache. It is what makes the JVM adaptable, and it is also what makes it slow to start and heavy for a small service.

2What native-image Does at Build Time

native-image moves most of that work to the build. It starts from the main method and performs a points-to analysis: it follows every call, field access and type that could be used from there, iterating until nothing new is found. The result is the set of reachable classes, methods and fields.

This is the closed-world assumption. The tool assumes that all code the program will ever run is known at build time. Classes that the analysis did not find reachable are simply not included, and no new classes can be loaded at run time.

The reachable code is then compiled ahead of time into machine code for the target platform, together with a small runtime called Substrate VM, which provides the garbage collector, thread support and exception handling. Some classes can be initialised at build time: their static initialisers run during the build, and the objects they create are written into an image heap, a pre-built part of the heap stored in the executable and mapped into memory when the program starts.

Shell
$ native-image -jar app.jar -o app
[1/8] Initializing...
[2/8] Performing analysis... [**********] # points-to analysis from main()
12,341 reachable types, 18,902 reachable fields, 61,775 reachable methods
[5/8] Compiling methods... # ahead-of-time compilation
[7/8] Creating image... # code plus image heap written out
Finished generating 'app' in 1m 52s.
$ ls -lh app
-rwxr-xr-x 1 dev dev 78M app # a single native executable
Step throughFrom jar to native executable
  1. Start from the entry pointmain method
  2. Points-to analysisfollow calls, fields and types
  3. Reachable set fixedclosed world: the rest is left out
  4. Build-time initialisationselected static initialisers run now
  5. Ahead-of-time compilationmachine code for the platform
  6. Write executablecode + image heap + Substrate VM

6 steps in order. Play them, or step through them one at a time.

Notice the counts: only what the analysis reached is compiled. A library with thousands of classes may contribute only a few hundred to the final image.

3Why It Starts Faster and Uses Less Memory

At startup, a native executable has nothing to load, verify, interpret or compile. The machine code is already there, and the image heap gives it pre-initialised objects for free. A small Spring Boot service that starts in 2 to 3 seconds on the JVM often starts in 50 to 150 milliseconds as a native image, and it runs at full compiled speed from the first request, with no warmup period.

Memory falls for the same reason. There is no class metadata for unused classes, no JIT compiler, no profiling data and no code cache to fill. The resident memory of the same service can drop from around 250 MB to 60 to 100 MB. For many small instances, scale-to-zero functions or command-line tools, that is a large saving.

Shell
$ time java -jar app.jar --server.port=0 & ... # JVM
Started App in 2.41 seconds (process running for 2.7)
RSS: 251 MB
$ ./app --server.port=0 # native image
Started App in 0.078 seconds (process running for 0.081)
RSS: 74 MB

The numbers depend on the application; the order of magnitude is what matters.

4Why Dynamic Features Need Metadata

The analysis can only follow what it can see in the code. Reflection such as Class.forName(name) with a name read from configuration, dynamic proxies created at run time, resources loaded by path and Java serialization all decide what they need while the program runs. The analysis cannot know that class com.example.OrderDto will be loaded by name, so it is not included, and at run time the call fails, for example with a ClassNotFoundException.

The fix is reachability metadata: files or code that tell native-image which classes need reflection, which interfaces need proxies, which resources must be bundled and which classes are serialized. Spring Boot's AOT processing generates much of this automatically, the GraalVM Reachability Metadata Repository provides it for many libraries, and we add the rest ourselves.

5Follow-up questions

Is a native image always faster than the JVM?

It starts faster and uses less memory, but its peak throughput is often lower than a fully warmed JIT, because the ahead-of-time compiler cannot use run-time profiles. Oracle GraalVM's profile-guided optimisation narrows that gap. For long-running high-throughput services, the JVM can still be the better choice.

What is the difference between build-time and run-time class initialisation?

With build-time initialisation, a class's static initialiser runs during the build and the resulting objects are stored in the image heap, so startup skips that work. It is only safe for classes whose static state does not depend on the run-time environment, such as the current time, random seeds or open files, which is why GraalVM initialises most classes at run time by default.

Can a native image load a plugin jar at run time?

No. Under the closed-world assumption, no classes can be loaded that were not part of the build. Plugin systems that load code dynamically need a different design, or must stay on the JVM.

6Conclusion

In this answer, we saw that native-image runs a points-to analysis from the entry point, keeps only the reachable code under a closed-world assumption, compiles it ahead of time with a small runtime, and can run some initialisation at build time into an image heap. With no class loading, interpretation or JIT warmup left for startup, the executable starts in milliseconds and needs far less memory, and dynamic features such as reflection, proxies, resources and serialization need metadata because the analysis cannot see them.

What to say in the interviewAbout 80 seconds aloud

Open with

GraalVM native image does most of what the JVM normally does at runtime during the build instead. The native-image tool starts at the main method and does a points-to analysis: it follows every call, field and type that could be reached, until it has the complete set of reachable code.

Then, if they want more

That is the closed-world assumption: whatever the analysis does not find is not included, and no new classes can be loaded later. It then compiles that code ahead of time into a native executable, together with a small runtime, Substrate VM, that provides garbage collection and threads.

Some classes can be initialised at build time, and their objects are stored in the image heap inside the executable. At startup there is no class loading, verification, interpretation or JIT compilation, so a Spring Boot service can start in under 100 milliseconds instead of a couple of seconds and runs at compiled speed immediately.

Memory is lower because there is no class metadata for unused classes, no JIT compiler and no profiling data. The cost is that anything dynamic, like reflection by name, dynamic proxies, resources and serialization, is invisible to the analysis, so it needs metadata, which Spring AOT and the reachability metadata repository mostly provide.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain what GraalVM native image does at build time and why it makes Java applications...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/729f127911/