NashTech Blog

Introduction to GraalVM: A Polyglot Virtual Machine

Table of Contents

Introduction To GraalVM

As technology advances rapidly, having modern and flexible runtime environments is more important than ever. These platforms are the foundation of today’s software, enabling developers to create, improve, and scale their applications quickly and efficiently. In a competitive world, being able to use multiple programming languages, enhance performance, and deploy across different systems isn’t just helpful—it’s crucial. This is where tools like GraalVM come in, offering a powerful way to build and run applications that help developers make the most out of their software.

Background

Initially conceived as a research initiative at Oracle, GraalVM has grown into a full-fledged commercial product. Officially launched in 2019 with its first production-ready release, GraalVM promises several advanced features. These include the ability to generate optimized, lightweight code, support for creating native executable binaries, and extensive polyglot capabilities, allowing seamless integration of multiple programming languages.

Twitter swiftly transitioned its Scala-based services to GraalVM for production use, reporting notable performance improvements. This switch resulted in lower operational costs and enhanced environmental sustainability by decreasing the number of servers needed in their data centers.

Technologies 

GraalVM’s Compilation Technologies: AOT and JIT

Just-In-Time (JIT) Compilation
  • Runtime Compilation:-:- Graal’s Ahead-of-Time (AOT) compiler compiles code dynamically at runtime.
  • Continuous Optimization: JIT continuously analyzes and optimizes code as it runs, improving performance based on actual usage patterns.

Summary of JIT Compilation

When we compile Java code with javac, it converts our readable code into bytecode, which is a condensed format understood by the JVM but not directly by processors. To actually run the program, the JVM interprets this bytecode. Since interpretation can be slow, the JVM employs a JIT compiler. This compiler translates bytecode into optimized machine code on-the-fly, ensuring efficient execution on the processor. This approach combines the portability of bytecode with the performance benefits of optimized native machine code execution.

Ahead-Of-Time (AOT) Compilation
  • Pre-compilation:- A technique in computer science where a program written in a high-level programming language is compiled into a lower-level language before the program is run known as a native image.
  • Fast Startup:- By preloading libraries and dependencies, AOT significantly reduces the application’s startup time, enabling near-instantaneous launch.

Summary of AOT Compilation

Ahead-Of-Time (AOT) compilation involves performing extensive static code analysis during the build process. It creates a native executable tailored for specific platforms and architectures, such as Windows, Mac, Linux, x64, and ARM. For example, this might result in an executable like Main.exe. This approach eliminates the need for bytecode interpretation or compilation when starting the program, enabling faster startup times.

However, the downside is that you must generate separate executables for each platform and architecture combination, contrary to Java’s original promise of platform-independent code.
AOT accelerates startup times and reduces overhead, while JIT ensures ongoing performance enhancements based on runtime behavior.

Native Image

Native Image is a cutting-edge feature provided by GraalVM Enterprise, allowing Java applications to be compiled ahead of time into a standalone native executable format. This executable, known as a native image, encompasses all necessary components: application classes, dependencies, JDK runtime libraries, and even native code linked statically from the JDK. Unlike traditional Java applications that require a JVM to interpret bytecode at runtime, native images are self-contained and can run directly on specific operating systems and hardware architectures.

In essence, GraalVM’s Native Image technology streamlines the deployment process by eliminating the need for a JVM during execution. This results in faster startup times and reduced memory footprint, making it ideal for cloud-native deployments, micro-services, and environments where efficient resource utilization is crucial.

By leveraging Native Image, developers can create highly optimized, self-contained Java applications that exhibit improved performance and efficiency across various platforms. This innovative approach not only enhances application responsiveness but also simplifies deployment and management in diverse computing environments.

Performance Benchmarks and Case Studies

We explore the performance differences between a traditional Spring Boot JAR and a native image generated with GraalVM. Starting with a Spring Boot application initialized using Spring Initializr, we added the native-maven-plugin to the pom.xml and configured GraalVM for native image creation. We built the application as both a JAR and a native image using Maven commands (mvn clean package for the JAR and mvn -Pnative native:compile for the native image).

First creates jar file

Create a native executable skipping the test

These are two file in target folders

we execute both the files and Observe the time difference

This jar file take 1.094 sec to start the application

At the same time native executable takes 0.046 seconds

Conclusion

In our performance comparison, the Spring Boot JAR took approximately 1.094 seconds to start, whereas the native executable achieved a significantly faster startup time of 0.046 seconds. This 96% reduction in startup time, although observed in a small-scale application, underscores the potential of native images for reducing latency and improving responsiveness, especially in environments requiring rapid deployments or handling micro services where frequent restarts are common. For larger applications, this performance enhancement can lead to substantial improvements in deployment efficiency and system scalability.

References

  • https://www.youtube.com/watch?v=FjRBHKUP-NA
  • https://www.baeldung.com/graal-java-jit-compiler
  • https://www.diva-portal.org/smash/get/diva2:1597213/FULLTEXT01.pdf
  • https://www.graalvm.org/latest/reference-manual/java/compiler/
  • https://www.graalvm.org/22.3/docs/introduction/
  • https://blog.nashtechglobal.com/benefits-and-usecase-of-graalvm-in-springboot/
Picture of rahulkrsinhaa

rahulkrsinhaa

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top