Boost Spring Boot Performance with GraalVM Native Images
This guide walks through installing GraalVM, adding the native‑image tool, creating a Spring Boot 2.4 project, configuring it for GraalVM, building a native executable, and comparing startup time and memory usage, showing how GraalVM can reduce startup from over a second to 0.2 seconds and memory from 480 MB to 25 MB.
What is GraalVM
GraalVM is a high‑performance virtual machine that can significantly improve program speed and efficiency, making it well‑suited for microservices. The popular Java framework Quarkus supports GraalVM out of the box, and Spring Boot 2.4 has begun adding GraalVM support to improve startup time, memory consumption, and response latency.
Installing GraalVM
The latest community version is 20.3.0, built on OpenJDK 8u272 and 11.0.9, essentially a derivative of OpenJDK.
The recommended installation tool is SDKMAN , which works like nvm for Node.js, allowing quick installation and switching of JDK versions.
Install a specific version and set it as default:
sdk install java 11.0.9.hs-adpt
sdk default java 11.0.9.hs-adptAfter installation, verify the version:
java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)Installing native‑image
native‑image is an ahead‑of‑time compiler from Oracle Labs that packages class dependencies and runtime libraries into a single executable, offering fast startup and low memory overhead. GraalVM does not include it by default; you must install it with the gu tool.
# Switch to the JDK installation directory
cd $JAVA_HOME/bin/
# Install native‑image
./gu install native-imageInitialize a Spring Boot 2.4 Project
Create a demo project with Spring Initializr:
curl https://start.spring.io/starter.zip -d dependencies=web \
-d bootVersion=2.4.1 -o graal-demo.zipRun the empty project to get baseline metrics (startup ~1135 ms, memory ~480 MB): java -jar demo-0.0.1-SNAPSHOT.jar Check memory usage:
ps aux | grep demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $11 "\t" $6/1024"MB"}'
/usr/bin/java 480.965MBEnable GraalVM Support
Add the required dependencies (Spring GraalVM native and context indexer) and the Spring Milestones repository to the Maven pom:
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-graalvm-native</artifactId>
<version>0.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
</dependency>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>Modify the main class to disable proxy bean methods:
@SpringBootApplication(proxyBeanMethods = false)
public class DemoApplication { ... }Build the native executable: mvn -Pnative package The build process is lengthy; logs show classlist generation and total build time of about 203 seconds.
The resulting executable is placed in the target directory:
Run the native binary instead of the JAR:
cd target
./com.example.demo.demoapplicationStartup time drops to ~0.215 seconds.
Memory usage falls to ~24.8 MB.
ps aux | grep com.example.demo.demoapplication | grep -v grep | awk '{print $11 "\t" $6/1024"MB"}'
./com.example.demo.demoapplication 24.8203MBPerformance Comparison
Without GraalVM: memory 480.965 MB, startup 1135 ms. With GraalVM native image: memory 24.8203 MB, startup 215 ms.
References
GraalVM – https://www.graalvm.org
Quarkus – https://quarkus.io
SDKMAN – https://sdkman.io/install
nvm – https://github.com/creationix/nvm
Gitee Gist – https://gitee.com/gi2/codes/famcqz6n21iylpg3us7j036
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
