r/java • u/Aggravating_Kale7895 • Jan 30 '26
How GraalVM can help reduce JVM overhead and save costs – example Spring Boot project included
Hi everyone,
I’ve been exploring GraalVM lately and wanted to share some thoughts and an example project.
The main idea is that traditional JVM apps come with startup time and memory overhead, which can be costly if you are running lots of microservices or cloud functions. GraalVM lets you compile Java apps into native images, which start almost instantly and use much less memory. This can lead to real cost savings, especially in serverless environments or when scaling horizontally.
To get hands-on, I built a Spring Boot example where I compiled it into a GraalVM native image and documented the whole process. The repo explains what GraalVM is, how native images work, and shows the performance differences you can expect.
Here’s the link to the repo if anyone wants to try it out or learn from it:
https://github.com/Ashfaqbs/graalvm-lab
I’m curious if others here have used GraalVM in production or for cost optimization. Would love to hear your experiences, tips, or even challenges you faced.
10
u/tealpod Jan 30 '26
We used GraalVM in production app, but our main aim is not just performance improvment but also to make it difficult to decompile code.
7
u/Zealousideal-Read883 Jan 31 '26
This is pretty sick.
If you're interested in taking this further, I would love to hear what you think about Elide, which is a runtime built on GraalVM that lets you skip the native image compilation step entirely while still getting the fast startup benefits. Link: https://github.com/elide-dev/elide
Like as a quick example off the top of my head, for kotlin scripts we're seeing 36x faster execution than kotlinc because we're using GraalVM's Truffle framework to execute .kts files directly instead of going through the traditional "compile then run cycle."
The multi-lang support is imo pretty underrated. it allows you to import and call TypeScript or Python libraries directly from Kotlin without spinning up separate processes or dealing with IPC/serialization. Everything runs on the same heap.
3
u/thewiirocks Jan 31 '26
I love the idea of GraalVM, but it tends to be impractical to deploy in most projects. While it can be worth it if you’re already stuck with the stack and have no better options for optimization, you can get better results by dropping Spring altogether and better engineering the solution.
For example, I’m looking at “top” on an actively loaded production application (~10-30 concurrent users) and it’s showing 301.4mb of memory usage. (Up slightly from the 295mb a few days ago.)
The app is deployed as an 42mb embedded Jetty JAR file with a startup time of about 2.6 seconds. I could probably cut that in about half by pre-extracting the JAR file, but the savings just isn’t worth the hassle.
4
u/todayiswednesday Jan 30 '26
Seems graal has had poor adoption and I’m not sure why
2
u/rbygrave Feb 01 '26
You can look at https://avaje.io/graalvm/ and https://avaje.io/graalvm/comparison
... which is a jvm vs native image comparison for an application in production.
13
u/lcserny Jan 30 '26
I abandoned my graalvm build, cause it was hard to know if something worked or not at runtime when it passed build and tests at compile time.
Unless you test everything using the native build also, you will have issues at runtime on some flow you forgot needed some classes that were excluded from the native build...
13
u/pron98 Jan 30 '26 edited Jan 30 '26
Note that most (not all) of the memory savings are due to a different default GC (Serial) and heap size configuration. If you configure the HotSpot VM the same way, you'll get most of the savings.
But remember that a lower RAM footprint always comes at the expense of higher CPU utilisation (this is a fundamental law of memory management), so pick your tradeoff carefully. It's better to let Java use more RAM to reduce CPU usage than to let it sit there wasted and unused. E.g. if a program uses most of the CPU but only, say, 50MB out of 500MB of free RAM, it is wasting 450MB that it could have used to reduce its CPU utilisation and handle a higher throughput. Using, say, 60% of the CPU but only 8% of RAM is just bad for efficiency.
2
u/iamwisespirit Jan 30 '26
Maybe you need to try graalvm + quarkus
4
u/Revolutionary-Judge9 Jan 31 '26
I use graalvm to build a native cli ai assistant (https://github.com/haiphucnguyen/askimo/tree/main/cli) for user does not need to install JRE in their local machine.
A key downside of GraalVM is that an application can run fine on the JVM, but the native executable may still fail at runtime. I must write many tests with tracing agent to collect all of meta data, and make sure it can cover all possible cases. I don’t think many teams are willing to adopt GraalVM and accept the risk of runtime errors from missing metadata in production unless they have a specific need for it, like my project.
1
u/Fiduss Feb 02 '26
GraalVM - I think nowadays its Quarkus native or just plain or "normal" setup
1
u/Stan_Setronica Feb 04 '26
1
u/Any-Spend-9417 Feb 07 '26 edited Feb 07 '26
GraalVM builds can definitely be frustrating at first, especially when you start dealing with native-image flags and reflection configuration.
Spring Boot helps a lot by shipping most of the necessary metadata, which already removes a big part of the pain compared to plain Java apps.
I ran into similar issues when experimenting with GraalVM, especially around keeping build configs consistent across profiles and CI.
That’s actually what pushed me to build a small CLI to orchestrate native-image builds on top of existing build tools, instead of relying on heavy plugins.
Still very early, but tools and examples like yours are really helpful for people getting started with GraalVM 👍
For anyone curious: https://github.com/Ariouz/GKit