Java / GraalVM Interview questions
How can you optimize the memory footprint of a native image executable?
A handful of build- and run-time levers reliably shrink both the executable's disk size and its runtime memory usage.
- Trim reachable code: unused dependencies and overly broad reflection configs pull in classes the analysis then has to keep and compile, so removing them shrinks the binary directly.
- Choose a lean GC: Epsilon GC (no collection at all, for very short-lived processes) or Serial GC use far less memory overhead than G1, at the cost of G1's better scalability for larger heaps.
- Strip debug info for production builds (omit
-g/keep it only for local debugging), since embedded debug symbols add meaningfully to binary size. - Tune heap flags at build time (
-R:MaxHeapSize) and launch time (-Xmx) to cap growth rather than relying on defaults sized for a general-purpose workload. - Use Quick Build Mode's opposite - the fully optimized build (default, not
-Ob) for production, since it also does more aggressive dead-code elimination than the quick-build path.
Measuring with -H:+PrintImageHeapPartitionSizes during the build shows exactly what's taking up space, which is far more reliable than guessing.
More Related questions...