Over the last couple of years we have witnessed the astonishing pace and scale of the innovations in the JVM ecosystem. Not only the new JDK release is coming out every 6 months, research and development are accelerating without any signs of lacking directions and features.
Table Of Contents
The idea for this tutorial was born out of the curiosity and confusion with respect to one of such developments, the GraalVM project, which went the route from the promising experiment to cutting edge technology. If this subject interests you as well, please join me on this journey where we are going to learn what GraalVM is and what it means for regular developers out there.
The official documentation contains the exact answer to this question however when you go over it, you may actually find the explanation somewhat vague and raising a number of questions.
The first thing we understood is that GraalVM is a runtime but what kind of runtime? And how does it outperform all other runtimes with respect to performance and efficiency? Last but not least, how does it shave away the differences between programming languages and their runtimes? Let us try to find the answers to these questions and, hopefully, demystify the GraalVM a bit.
For every supported programming language, GraalVM emulates the behavior of its default runtime and the executables of all language runtimes come bundled with the GraalVM distribution (which we are going to cover shortly). But how come GraalVM emulated runtimes perform better than the default, language-native ones?
The heart and soul of GraalVM is the GraalVM Compiler – a dynamic just-in-time (JIT) compiler that aims to improve the efficiency and performance characteristics of the applications using extensive code analysis and advanced optimization techniques. Although the GraalVM Compiler is written in Java, it uses intermediate representation (IR) in order to perform language-independent optimizations and generate machine code. When ran as a shared runtime, such unique capabilities augment GraalVM to a truly polyglot platform.
Above all, GraalVM comes with a special, privileged treatment of JVM runtime. First and foremost, the GraalVM Compiler integrates with Java HotSpot VM using JVM Compiler Interface (JVMCI), delivered as part of JEP 243. But GraalVM goes even further, incorporating its optimizing compiler into native image – an advanced ahead-of-time (AOT) compilation technology to translate Java (and other JVM languages) code into a native platform executable.
Although it sounds exciting, let us stop and reflect for a minute. We know that the GraalVM Compiler is written in Java, so when it is integrated with Java HotSpot VM, it must pass through JIT first, before it can do anything useful, right? Indeed, it used to be the case but not anymore thanks to libgraal, a shared library produced by native image that contains a pre-compiled version of the GraalVM Compiler!
Is GraalVM this kind of marvelous product of software engineering that is going to change the ways we used to run our applications? Or it is yet another marketing attempt to sell as the “silver bullet” buzzword?
Personally, I am not good at predicting the future, probably like most of us. Nonetheless, it is clear that the GraalVM is a combination of evolutionary and revolutionary ideas. For example, the seamless interoperability between different programming languages using the same highly optimized shared runtime opens tremendous amount of unforeseeable opportunities.
But probably the real winner, at least in short term, is Java and the JVM platform. The HotSpot VM comes with its own highly optimizing bytecode C2 compiler, written in C++. Unfortunately it has been mentioned in many sources that over the years it has become exceptionally complex and difficult to maintain, all the more improve. The GraalVM Compiler could be seen as next generation compiler for HotSpot VM with a potential to become a complete C2 compiler substitution in the future. May not seem like a revolution but certainly a so needed evolution.
The ahead-of-time (AOT) compilation and native image is a completely different story though. It brings JVM to the edge of cloud native and serverless computing where its place was not imaginable before. This is by all means a breakthrough for JVM ecosystem.
“ … we believe that GraalVM finally is mature and ready for production use. … “https://medium.com/graalvm/announcing-graalvm-19-4590cf354df8
GraalVM distribution is available in two editions: Community and Enterprise. The community editions are based on OpenJDK versions whereas the enterprise ones are based on Oracle JDK and require paid subscription. In both cases, Java 8 and Java 11 distributions are available.
Along the tutorial we are going to use the community edition only. As of this moment, 20.3.0 is the latest GraalVM Community release which is based on OpenJDK version 1.8.272 and on OpenJDK version 11.0.9, depending which bundle you need. The GraalVM Community distributions for all major supported platforms could be downloaded from the project’s official Github page.
In the next section of the tutorial we are going to discuss all the goodies which GraalVM brings in the context of JVM applications.