loading...

GraalVM - Polyglot Virtual Machine

abdulfousan profile image Abdul Fousan ・4 min read

It seems like the whole Java ecosystem is going to adopt GraalVM in these days. Every library, framework and even community speakers, developers also wants to proudly work on GraalVM, that makes GraalVM is essential for modern Java applications.

Where it comes from?

GraalVM is an extension of the JVM written in pure Java, supporting polyglot programming and ahead-of-time compilation developed by Oracle. Since many years Java virtual machine maintained and distributed by Oracle to run Java programs. Java virtual machine is written mainly in C/C++. The main purpose of the JVM is to run Java bytecode (.class files) and continuously analyze the program’s performance for so-called hot spots within the program which are executed often and to just-in-time (JIT) compile them to native code (machine code) for improved performance. This is done during runtime rather than prior to execution of the Java program and therefore just-in-time.

The Graal OpenJDK project demonstrates that a compiler written with pure Java can generate highly optimized code. With this AOT-compiler will result in improved start-up time as the JIT compiler does not have to intercept the program’s execution.

Oracle started to develop the GraalVM to not only a faster JVM but also tackle the current polyglot movement with a virtual machine written in Java. The architecture of the GraalVM looks like the following:

Alt Text

What actually GraalVM do?

"GraalVM – a standalone Java Development Kit to execute Java or JVM-based languages (e.g., Scala, Kotlin), dynamic languages (e.g., JavaScript, R, Ruby, R, Python), WebAssembly, LLVM-based languages (e.g., C and C++) in one shared runtime." -- From GraalVM website

Features

  • Ahead-of-time(AOT) compiled native images
  • The Truffle Language Implementation framework to implement third-party language support
  • Low memory footprint, Make java faster
  • Polyglot support

Inside GraalVM Distribution

GraalVM compiler — written in Java and supports both dynamic and static compilation
OpenJDK — Polyglot API & Isolates API
Truffle framework — a language implementation framework for creating languages and instrumentations for GraalVM
LLVM bytecode interpreter
JavaScript interpreter — an ECMAScript compliant JavaScript engine
Java HotSpot VM — runtime with the GraalVM compiler enabled as the top tier JIT compiler for JVM-based languages
Node.js — runtime with the GraalVM JavaScript interpreter enabled as the JavaScript engine.
Native Image tool — the framework that allows ahead-of-time (AOT) compilation of Java applications under closed-world assumption into executable images
FastR — GraalVM implementation of the 3.5.1 programming language
GraalPython — GraalVM implementation of the Python 3.7 programming language
TruffleRuby — GraalVM implementation of the Ruby 2.6.2 programming language
SimpleLanguage — A simple demonstration language for the GraalVM
GraalVisualVM — a visual tool integrating command line JDK tools and lightweight profiling capabilities
Ideal Graph Visualizer — is developed to view and inspect interim graph representations from GraalVM and Truffle compilations
GraalWasm – the WebAssembly (Wasm) language interpreter to run Wasm programs in the binary format.

Why do people put so much interest?

I guess people are more interested because of faster startups and cloud native images excites people so much about to use GraalVM and that compiles JVM applications to native executable makes distribution is simple.Once you compile a JVM application into a native executable, it can run without a JVM.

This is interesting in a container world since the process starts fast, and since there is no virtual machine there is no code to generate with a JIT, and the process uses much less memory (a traditional issue for JVM in a memory-capped environment).

GraalVM's native images in serverless environment are super fast and able to showcase java is still on track with other new modern programming languages.

Install GraalVM Community Edition on Linux(Ubuntu)

1) Download the new release of GraalVM and unpack it anywhere in your filesystem:

$ tar -xvzf graalvm-ce-java11-linux-amd64-19.3.1.tar.gz

2) Move the unpacked dir to /usr/lib/jvm/ and create a symbolic link to make your life easier when updating the GraalVM version:

# mv graalvm-ce-java11-linux-amd64-19.3.1/ /usr/lib/jvm/
# cd /usr/lib/jvm
# ln -s graalvm-ce-java11-linux-amd64-19.3.1 graalvm

3) Add a new alternatives configuration. First grab the priorization number by listing the already installed JVMs and then use this number to configure the new one:

# update-alternatives --config java

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
* 1            /usr/lib/jvm/jdk1.8.0_231/bin/java               0         manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

In this case I have 3 java alternatives installed, so I'm going to install the fourth.

# update-alternatives --install /usr/bin/java java /usr/lib/jvm/graalvm/bin/java 4

To make sure everything is working fine, set the new JVM on your environment:

$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/jdk1.8.0_231/bin/java               4         manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
* 3            /usr/lib/jvm/graalvm/bin/java                    0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 3

To verify, just check the version number:

$ java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b06)
OpenJDK 64-Bit GraalVM CE 19.3.1 (build 25.242-b06-jvmci-19.3-b07, mixed mode)

And you're set.

Note

The native-image executable is not bundled in the GraalVM distribution anymore. Install it manually using $GRAALVM_HOME/bin/gu install native-image.

Conclusion

I hope that you have enough information to help you get started and Oracle will continue to invest time in this great project and makes polyglot language support becomes production-ready soon combined with its native image capabilities.

Have fun!

If you like this article, please leave a like or a comment.

Posted on by:

abdulfousan profile

Abdul Fousan

@abdulfousan

Full Stack Developer. ¯\_(ツ)_/¯ Mainly working in Java and building mobile apps, sometimes doing both at once. Eager to explore, learn and create something new.

Discussion

markdown guide
 

I still cross my fingers for GraalVM's success. Have you noticed any performance benchmarks on JavaScript running on GraalVM vs. standalone Node.js?