Polyglot and Native runtime with GraalVM

GraalVM is a universal VM mainly for Java and JVM supported languages. With GraalVM you can run native programs written in Python, Ruy, R and all JVM languages like Scala, Clojure, Groovy and Kotlin also LLVM based like C and C++. Graal VM removes isolation between languages and enables interoperability with natively shared runtime. You might be wondering why botter use other VM? Well, there are a couple of good reasons. Java is a pretty old language and does not have the best startup time. Today we are all running services in containers on top of kubernetes and we are always looking for ways to improve the customer experience. Startup time is not only a matter of better customer experience but also availability. For the developers, this means better development experience and idiomatic experience with the best solution for each specific problem.

GrralVM Supported Runtimes

GraalVM can be used standalone runtime or embedded in the context of OpenJDK, Node.JS, Oracle Database or MySQL. JVM has a lack of optimizations, for a long time we don't see great compiler optimizations besides some contributions from Intel. For many folks, the compiler is dead a long time already. GraalVM comes to rescue with a Modular Design, Easy to understand, great in-lining, great scape analysis.

GraalVM Benefits

It's important to mention that Twitter is using GraalVM in production.

The main Benefits of using GraalVM are:

  • High-Performance modern Java VM
  • Low-Footprint, Faster Startup in Java
  • Run Native Languages on the JVM
  • Polygot: Combine Java, Groovy, Scala, Clojure, Kotlin, C, C++, Ruby, Python, R.
  • Java code as native binary
  • You can Extend the JVM and also create your own language.


I'm not 100% sure if GraalVM can perform better than V8 however there are lots of benchmarks showing that GraalVM has better performance than other JS JVM runtimes. Comparing GraalVM with standard VM there is a huge gain.  Twitter saved 11% CPU using Graal instead of C2 and almost 3% fewer GC pauses which might sound little but when you are in the scale of Twitter with hundreds of microservices this makes a lot of difference.

Java program in around three-quarters of the time it takes to run it with a standard HotSpot compiler
There are some known limitations in sense of support for Java. The classes must be all available during compilation time, you can see the limitations here.

For this Benchmark, we can see 7 times less memory usage with netty and GraalVM compared with regular JVM. Native image executable is less than 9MB and has significantly better startup time.

Netty Benchmark with Regular JVM vs GraalVM (source)

10x Improvement in Cold start (Scala on GraalVM)

Scala language always had issues with build time. With graalVM compilation time improved a lot. Take a look in this benchmark:

ScalaC compiler Faster with GraalVM (source)

Calling Java and R from Node.js

Here is a simple sample code. You can see how is easy to call other languages with GraalVM.

Complete source here.

Build Native with Gradle

In order to use GrallVM gradle plugin, we will need to use this plugin. Let's get started and see the build.gradle file first.

As you can see here we imported the plugin and at the end, we are defining the main class of the application, output name which is the native binary name and for last the version of the GraalVM.

Now let's take a look at the java code. Here we have a simple java code, pretty much the hello word classical sample.

Now we can generate the native binary doing:

./gradlew build nativeImage

You should see something like this:

Them you can simply run the code running the binary.

hello, world from GraalVM!

The complete source code is here. GraalVM still has a bright future ahead with Oracle and Twitter. Recently IBM Red/Hat JBoss release a Java cloud-native microservice solution(A spring-boot like framework but with Maven and JEE) which also uses GraalVM named Quarkus.

Diego Pacheco

Popular posts from this blog

Podman in Linux

Java Agents

Manage Work not People