GraalVM What is it?

Features of GraalVM

GraalVM is a high performance JDK with a focus on 3 core features - high performance, compiled native app and polyglot programming. It is a drop-in replacement for Java 11 and Java 17. GraalVM community edition is an open source project with over 3.6 million lines of code contributed by the GraalVM team and collaborators.

GraalVM adds the following things to the Java HotSpot VM,

  • A GraalVM compiler that is capable of compiling applications to a native image that is built for the underlying operating system. This ensures high performance and quick to start applications suitable for cloud and container platforms. With static linking of system libraries, a static binary can be even run on a bare-metal docker image!
  • A language implementation framework codenamed Truffle that makes it possible to run programming languages such as JavaScript, Ruby, Python etc. on top of JVM. Ability to interoperate across languages under a shared runtime makes GraalVM a true polyglot programming environment.
  • GraalVM LLVM runtime that can execute programming languages that can be converted to LLVM bitcode. This includes languages such as C, C++ and Fortran.
  • A low overhead language agnostic fully dynamic instrumentation support available as part of the VM runtime. This can be used by API clients. It can track very fine-grained VM-level runtime events to inspect, profile and analyse the runtime behaviour of applications running on GraalVM.

The key vision statement of GraalVM is,

"Abstractions should be without performance regret!"

As per Oracle’s initial announcement, GraalVM can provide up to 100x faster startup time and up to 5x reduction in memory usage by using the ahead-of-time native compilation. This makes it a very tempting choice for building cloud native microservices.

GraalVM Architecture Diagram

The following diagram shows the high level layered architecture of the GraalVM ecosystem supporting native image compilation and polyglot features,

GraalVM Architecture Diagram

GraalVM is a project managed by Oracle and provides both enterprise and community edition distributions.

Goals of GraalVM Project

  • Improve the performance of Java VM languages to match the performance of native languages and ensure low memory footprint.
  • Reduce the startup time of JVM applications by compiling them ahead-of-time to native code.
  • Enable GraalVM to be an embeddable technology, for example inside Oracle database.
  • Allow use of multiple programming languages in a single program, the polyglot way.
  • Enable application extensibility by supporting reuse of libraries from other languages or building language agnostic tools.

A Brief History of GraalVM

The word Graal comes from the old French for Grail (a thing to be eagerly pursued). Graal project started as a Java compiler research project inside Oracle Labs under the leadership of Thomas Wuerthinger. The original goal was to create a faster compiler for Java but then evolved to build additional features. These included ahead-of-time compilation called native image technology supporting fast native binaries and a language implementation framework enabling polyglot capabilities using non-JVM languages such as Python, Ruby and WebAssembly. All the technologies under Graal were released under the single umbrella name GraalVM.

The GraalVM ecosystem was in development for years in Oracle labs. In June 2018, a release candidate (1.0-RC2) version was released under free community edition. The first production ready version was released by Oracle under GraalVM enterprise edition 19.0 in May 2019 as a drop-in replacement for JDK 8. In October 2022, Oracle announced GraalVM JIT and native image becoming part of OpenJDK.

Following shows the evolution of GraalVM from May 2019 to October 2022. These versions are available either in community edition or enterprise edition with enterprise edition having additional features.

GraalVM 19.x versions (May 2019 to April 2021)

  • Added support for JDK 11
  • Improvements to GraalVM compiler
  • Upgrades to Truffle language runtimes

GraalVM 20.0.x to GraalVM 20.3.x (February 2020 - January 2022)

  • Full support for Windows platform
  • Improved native images
  • Upgrades to Truffle language runtimes
  • Improved tooling support

GraalVM 21.0.x to GraalVM 21.3.x (January 2021 - September 2022)

  • Java on Truffle
  • GraalVM community images from Github container registry
  • Linux ARM 64-bit support
  • Serialisation support for images
  • Tooling and language runtime updates
  • Java 17 support

GraalVM 22.0.x to GraalVM 22.3.x (January 2021 - October 2022)

  • Dropped support for Java 8. Now supports JDK 11 and 17 only
  • Native image optimizations reduced executable size
  • Updated to Truffle language implementations
  • Added support for Apple silicon ARM M1

Limitations and Disadvantages of GraalVM

There is a lot of hype in the Java ecosystem about GraalVM. However, it is important to be aware of the limitations and disadvantages of GraalVM before adopting it. The ability of GraalVM to create native executables using SubstrateVM while exciting also comes with certain limitations. It is important to know these before deciding to adopt native images as your solution format,

  • Java can use reflection to dynamically load classes. This is a problem for native compiler since it needs to know all the classes that may be loaded at runtime to generate the native image. The workaround is to specify all the classes needed using the native image configuration files.
  • A lot of third party libraries and frameworks use reflection heavily. Hence those need to support GraalVM explicitly for it to work. See Spring Native.
  • The native executables created do not have a JIT compiler. Hence aggressive code optimisation at runtime using profiler and JIT is not possible.
  • Community edition of GraalVM provides a serial garbage collector as part of the native image runtime. It is intended for low memory footprint and small Java heap sizes. The runtime behaviour in this case can be different from a JVM based GC.

Language and Runtime Support in GraalVM

GraalVM is designed for the Java ecosystem and hence can execute all languages that compile to the Java bytecode format. These include languages such as Java, Scala, Kotlin etc.

In addition, the Truffle Language Implementation Framework supports the following additional languages,

  • JavaScript - ECMA 2021 JavaScript runtime with NodeJS support
  • Ruby - Ruby language implementation with support for Ruby on Rails
  • R - R language implementation
  • Python - Python 3 language implementation
  • WASM - WebAssembly implementation
  • LLVM - Sulong LLVM bitcode interpreter implementation

Who Uses GraalVM in Production?

Due to GraalVM’s diverse toolset and cloud advantages, some of the big players in the tech industry are currently using GraalVM in production. These include Facebook, Oracle, NVidia, Twitter, Goldman Sachs etc. Some of the examples of GraalVM in production are given below,

  • Facebook with Spark workloads - Facebook has huge data sets and Java is used in big data processing using technologies such as Spark. By adopting GraalVM, Facebook was able to get reduced CPU and memory utilisation for spark workloads. Facebook using GraalVM for more details.
  • Twitter Microservices Platform - Most of Twitter’s microservices are written in JVM language Scala. Using the GraalVM compiler, the CPU consumption was reduced more than 10%. The key technical features behind this improvement are better code inlining and escape analysis by the new compiler. Twitter using GraalVM in production.
  • Goldman Sachs Slang Language Migration - In this interesting use case, Goldman Sachs is using the GraalVM’s Truffle language implementation framework to modernise their in-house programming language called Slang (Securities Language) with ability to interoperate with legacy C++ code. Team found tooling, native interoperate and documentation as key good parts of the GraalVM ecosystem. GoldMan Sachs Using GraalVM in Production for more details.

What You Can Do with GraalVM?

  • Use it as a platform for high performance modern Java.
  • Use it in container or cloud environments as a low footprint and fast startup Java microservices.
  • Use multiple languages such as Java, JavaScript, Ruby etc. for a single application.
  • Run native languages such as C, C++ on the JVM.
  • Create tools that work across all languages.
  • Extend a JVM based application.
  • Extend a native application.
  • Build Java code as a native library.
  • Implement polyglot features in database.
  • Create your own language.

GraalVM - Additional Reading