Sub topic 9
Garbage collection and memory in a native image
This chapter assumes you can build a native executable with native-image and know what a JVM heap, a garbage collector and JIT compilation do. It covers how a native image manages memory without a JVM and which settings you can change.
What changes without a JVM
A native executable still has a managed heap and a garbage collector. The difference is where they come from. The collector and the runtime support code are written in Java and compiled into the executable by Native Image. This runtime is called Substrate VM. At run time there is no class loader, no bytecode interpreter and no JIT compiler.
Three things follow from this:
- The heap starts small, because no JVM, JDK class data or JIT-compiled code has to be loaded first.
- Objects that were created during the build can be stored in the image itself, in the image heap.
- The collector is fixed at build time. You cannot switch it with a flag when you start the program.
The image heap
The image heap is a region of the executable that holds objects prepared at build time. Examples are string literals, class metadata and any static field values from classes you chose to initialize at build time (see the chapter on class initialization). At startup the operating system maps this region into memory, so the program does not have to rebuild those objects.
The image heap is read from the file, so it counts toward the executable's size. Large static data structures therefore make the binary bigger, and they are present from the first instruction.
Choosing a garbage collector
Native Image offers these collectors:
| Collector | Option | Notes |
|---|---|---|
| Serial GC | default | Single-threaded, stop-the-world, low overhead. |
| G1 | `--gc=G1` | Parallel and concurrent, aimed at lower pause times on larger heaps. |
| Epsilon GC | `--gc=epsilon` | Never frees memory. For very short programs and testing. |
Which collectors exist depends on your GraalVM version, platform and edition. G1 has been limited to Linux on certain architectures. Check the documentation for your release before you rely on it.
Serial GC suits small services and command-line tools. For a long-running service with a large heap and many cores, G1 is the usual candidate. Measure both.
A build with a different collector looks like this:
native-image --gc=G1 -jar app.jar -o appSizing the heap at run time
Many heap options for a native image are passed to the executable itself, using the -X prefix. They are not passed to native-image.
./app -Xmx256m -Xms64m-Xmxsets the maximum heap size.-Xmssets the initial heap size.-Xmnsets the size of the young generation.
If you set no maximum, the default is a fraction of physical memory. In a container, the runtime reads the container's memory limit. Even so, set -Xmx explicitly for production. A heap that grows to its default limit can push the process past the container limit and cause the operating system to kill it.
You can also bake defaults into the executable at build time:
native-image -R:MaxHeapSize=256m -jar app.jar -o appThe -R: prefix passes a run-time option into the image as its default value.
Observing collections
To see what the collector is doing, run the executable with its logging option:
./app -XX:+PrintGC -XX:+VerboseGCEach line reports a collection and how long it took. If you see frequent collections of a nearly full heap, the heap is too small for the workload.
Memory differences to expect
- Lower resident memory at startup. There is no JIT compiler or code cache, and no class metadata being loaded.
- Heap behaviour is less adaptive. The JVM tunes itself using run-time profiles. A native image follows fixed policies.
- Memory outside the heap still exists. Thread stacks, native libraries and direct buffers are not part of
-Xmx. Total process memory is higher than the heap limit.
Where memory behaviour is decided
Common mistakes
- Passing
-Xmxtonative-imageinstead of to the executable. It sets the memory of the build process, not of your program. - Expecting to switch collectors at run time. Rebuild with a different
--gcvalue. - Using Epsilon GC for a program that allocates continuously. It will run out of memory.
- Setting the container limit equal to the heap limit.
Exercise
A native image of a service is killed by the operating system in a container with a 512 MB limit. The service runs with -Xmx512m. Give the likely cause and a fix.
Show answer
-Xmx limits only the Java heap. Thread stacks, native libraries and direct buffers use extra memory, so the process exceeds 512 MB in total. Lower the heap, for example to -Xmx384m, or raise the container limit, so that there is headroom for non-heap memory.
Exercise
You want to try G1 instead of the default collector. At which step do you make the change, and which option do you use?
Show answer
At build time. Run native-image with --gc=G1 (if your GraalVM version and platform support it). The collector is compiled into the executable and cannot be switched when the program starts.