The global IT industry is well into the Cloud Age, and while companies eagerly adopt cloud migration strategies, the demand for a versatile, highly performant runtime increases.
A perfect runtime environment enables developers to use the full potential of any programming language on one platform. At the same time, it increases the performance of an application and minimizes footprint. Wouldn’t the search for such runtime be like a chase for the Holy Grail? Well, there’s no need for chasing the Grail when you can build one — welcome to GraalVM!
Table of Contents
What is GraalVM
GraalVM is a Java Virtual Machine (JVM) and Java Development Kit (JDK) written in Java. It aims to accelerate JVM-based applications and introduces other programming languages into the project, such as JavaScript, C/C++, Python, etc.
Architecture
GraalVM is based on the HotSpot VM, but unlike HotSpot C1/C2 compilers developed in C/C++, it uses its own Graal Just-in-time (JIT) compiler written mainly in Java, which enhances its modularity and maintainability. A layer of the JVM Compiler Interface (JVMCI, JEP 243) allows the JVM to use the Graal Compiler as a dynamic compiler.
Graal JIT compiler includes numerous optimizations: more aggressive inlining, data flow analysis, and better escape analysis, to name a few. As a result, it provides superior performance compared to C1/C2 compilers.
In addition to the Graal Compiler, GraalVM includes the Truffle language implementation framework (or Truffle), making it possible to work with non-JVM languages, such as Python, JavaScript, and others. Truffle is used for creating language interpreters and compiling them with Graal JIT Compiler so that the languages can run on the same JVM and exchange data. It implements a polyglot interoperability protocol — a set of messages implemented by programming languages — allowing smooth communication within multilingual apps. Truffle wasn’t initially developed to execute Java code, but now you can run Java applications using Java on Truffle mode for seamless Java interoperability with other languages.
Finally, developers can utilize native languages such as C/C++ on GraalVM thanks to Sulong — an LLVM bitcode interpreter.
GraalVM’s architecture can be schematically depicted as follows:
Architecture of GraalVM
Native image
A powerful addition to GraalVM is the native image technology enabling Ahead-of-time (AOT) compilation instead of JIT. To understand why it matters, we must brush up on the mechanisms of compiling JVM-based apps.
Just-in-time (or dynamic) compilation is the process of converting the JVM bytecode into machine code after the program starts, i.e., at runtime. The JIT compiler analyzes the code, searches for hotspots (the most frequently used code parts), decides which OS the app runs on, etc. After that, the compiler performs multiple optimizations to produce highly performant code. JIT compilation is perfect for long-running, high-throughput applications. But it has certain drawbacks, namely a larger footprint and increased startup time.
With AOT (or static) compilation, the bytecode is converted into machine code before the program execution. The AOT compiler eliminates dead code and creates an executable specific to the OS and architecture, which accelerates the startup and optimizes memory consumption.
The resulting native image starts up almost instantly and reaches peak performance without warm-up. In addition, native images don’t require JVM to run, providing a smaller footprint. However, AOT can’t handle the dynamic features of Java and can’t perform some optimizations such as adaptive optimization or function inlining. So it is suitable for applications where fast startup is a primary goal.
GraalVM advantages
Why do we need another JVM? Isn’t the good old HotSpot good enough? True, properly tuned HotSpot can provide excellent performance for various use cases. Originally, the primary objective of the GraalVM project was a more performant, easy-to-maintain, and enhanced JIT compiler. But with the outspread of microservices and cloud computing, GraalVM has revealed other significant advantages:
- Native images with next-to-instant startup eliminate the issue of cold starts in the cloud, which is especially important for Amazon AWS Lambda (and similar services from other cloud providers). Amazon Lambda is a pay-per-use function running the code in response to events only. The service is shut down when nobody uses the app and has to be started again when triggered. Traditional JIT compilation needs time for optimizations so that a cold start can stretch out for several seconds, irritating users and leading to lost profit. Graal AOT compilation gets the benefit of cost-efficient Lambdas and, at the same time, enhances customer experience
- GraalVM makes extensive polyglot projects a reality. Companies create microservices using any programming language and have no trouble establishing communication between them. Furthermore, it is possible to integrate packages and libraries from different languages fitting perfectly into a GraalVM-based app. For instance, a Java application can use R packages for enhanced data processing or Python for machine learning functions.
- Applications boosted with Graal JIT capabilities are fast, provide higher throughput, and require fewer resources, which translates into lower cloud bills.
Not only cloud-native applications benefit from GraalVM. JavaFX programs compiled into native images don’t require Java installed on the machine to run. The size of the executable can be seven times less than a traditional jar with JRE, and it also starts much faster.
The cherry on top of the GraalVM cake is that it is based on OpenJDK and Oracle JDK, so the migration will not be associated with code changes.
GraalVM is gaining popularity
GraalVM is young, powerful, and full of energy and ambition. No wonder it has gained so much traction in recent years. Major Java frameworks have adopted the technology:
- Quarkus
- Micronaut
- Spring Boot
- Helidon
Note that Spring Native has been integrated in Spring Boot 3, which means it recieved a baked-in support for GraalVM Native Image starting with version 3.0.0-M5.
In addition, major industry players — Twitter, Alibaba, Facebook, Oracle Cloud Infrastructure (OCI) Monitoring service, and many others — integrated GraalVM into their workloads and reported better performance and decreased memory and CPU consumption.
GraalVM is an open-source project, and given that it is written in Java and has a cleaner code base compared to C-based HotSpot with 20 years of history, it is much easier to optimize and enhance. Indeed, innovations are fast to arrive. Every quarterly GraalVM release brings something new to the community. For instance, the latest 22.3.0 version contains the following Java updates:
- Added support for JDK 19 and JEP 425: Virtual threads enabling the development of high-throughput concurrent Java applications
- Improved JIT compilation isolation to reduce GC pause times
- Improved the debugging process for memory leaks identification
- Reduced the size of the Native Image installable by shipping the LLVM backend as a separate bundle
- Other enhancements
Any developer can technically contribute to GraalVM. OpenJDK has already proven the viability of the open-source philosophy and enjoys constant fixes and enhancements from an extensive developer community. But when large companies are interested in the project, it indicates great prospects for the solution. Industry leaders such as Oracle, Red Hat, BellSoft, Alibaba, etc., engage in driving GraalVM forward by bringing new features and innovations to the project. For instance, BellSoft engineers have recently prepared a work-in-progress GitHub pull request to implement Parallel GC to GraalVM Native Image, which will be a significant step towards concurrent garbage collection within the native image.
How to use GraalVM
There are two editions of GraalVM: GraalVM Community based on OpenJDK and GraalVM Enterprise based on Oracle JDK. GraalVM CE is open source and, therefore, free to use but offers no commercial support. GraalVM EE comes with 24x7 support and additional features that haven’t made it to the community edition yet, such as support for Apple Silicon or software bill of Materials (SBOM). In addition, GraalVM EE includes the G1 garbage collector absent in CE.
Getting started with GraalVM is simple. Choose the OS on the official site and follow the installation instructions to download the core distribution with JVM and Graal Compiler. It will enable you to run Java apps. Runtimes for other languages can be installed with the gu tool.
It is also possible to use the native-image function separately from the main GraalVM distribution. Although the AOT compilation, added as an experimental feature to Java 9, was removed from Java 16, it is provided as part of standalone GraalVM-based solutions. Liberica Native Image Kit is one of them.
Liberica Native Image Kit — a powerful GraalVM-based solution
BellSoft is actively involved in GraalVM development, being part of the GraalVM Project Advisory Board and contributing fixes and improvements. So we used the high-level expertise of our engineers to create Liberica Native Image Kit (NIK), a utility based on GraalVM Community Edition for native image generation. The Spring team trusts the quality and reliability of Liberica and utilizes it as the default native-image compiler in Spring Boot.
So what do you get with Liberica NIK?
- The tool is always based on the latest version of Liberica JDK 11 & 17 and GraalVM, with security patches, bug fixes, and improvements for you to enjoy the max. security and fresh features at all times
- Wide range of supported platforms — Linux, macOS, Windows — including musl and glibc support, so you can use Liberica NIK with a musl-based OS (for instance, Alpaquita Linux) and drastically reduce the size of your containers
- Three flavors are available for enhanced flexibility: Core, Standard, and Full. The Core package contains only Liberica VM and native image for Java development. The Standard package supports language installables for polyglot projects. The Full package includes LibericaFX based on OpenJFX to develop rich web and desktop applications. Choose the package that best suits your needs and save memory resources.
- Liberica NIK is an open-source utility and can be used for free. But we also provide commercial enterprise-grade support with 24/7 access to our experts, emergency security patches, and prompt fixes based on SLA The support also covers Liberica JDK, a progressive Java runtime with the widest range of supported system configurations, or Alpaquita Cloud Native Platform, a technology stack for cloud-native Java applications. ACNP is based on Alpaquita Linux, a minimalistic, performant, and secure containerized Linux, highly efficient with Java and Native Image workloads.Learn more about Alpaquita Linux
But less talk, more action! You can try out Liberica NIK right now and see how it fits perfectly into the existing infrastructure and helps launch the apps in a fraction of a second!
Furthermore, take a look at our guides with step-by-step instructions on integrating Liberica NIK with the most popular Java frameworks or turning JavaFX apps into native images:
- Generate a native executable with Spring Native
- Create a microservice with Micronaut framework
- Build a native image with Quarkus framework
- Learn to convert JavaFX applications into native images
Conclusion
GraalVM paves the road to the world of cloud-native polyglot programming. It uses Java to build a sustainable, maintainable, and highly performant platform that can take in any programming language to use its strengths for the sake of your project.
Embrace the innovation now! Install GraalVM or experiment with native images. See how Liberica NIK transforms your services into lightweight, high-speed, performant executables and puts Java on par with Go or Rust in terms of Cloud costs. At the same time, it enables you to use all the best practices, tools, and developers expertise.
We understand that introduction of Native Image requires deep code refactoring, so large companies hesitate with migration. Fortunately, there exists a simpler way to save Cloud resources — Alpaquita Cloud Native Platform. Click on the button below to learn more about the solution that will help you immediately cut the expenses.