Where does the name “GraalVM” come from?
The word “Graal” comes from old French for “Grail.”
The “VM” in “GraalVM” comes from the fact that it runs inside the JVM.
Current Situation
When you first run your application, JVM interprets code and collects “profiling information” (about which code branches are executed most often, how frequently loops are executed, and which types are used in polymorphic code). This is not as fast as running compiled code.
Solution: Oracle’s JVM (HotSpot)
- Hotspot VM also contains just-in-time (JIT) compilers.
- JIT compilers compile your application code to machine code on the go, as your program executes.
- If your code gets frequently executed (warms up), it gets compiled to machine code by the C1 (Client Compiler) JIT compiler.
- If it is still executed often enough, reaching certain thresholds, it is compiled by the top-tier JIT compiler (C2 (Server Compiler) or the Graal compiler).
- As well as “profiling information” based optimizations, sometimes the compiler performs “speculative optimizations”.
- The assumptions can become invalid at a later time (since code execution is dynamic).
- The optimized, compiled version of a method based on the profiling information can be reverted back to interpreted mode.
👎🏻 Complex computations that require significant CPU time.
👎🏻 JVM requires considerable memory to store profiling information, and requires appreciable time and memory to start.
Solution: GraalVM
GraalVM
- A high performance optimizing JIT compiler (C2 or the Graal compiler, to accelerate performance of any Java and JVM-based application without the need for code changes)
- An ahead-of-time (AOT) compiler to build native executables (to compile JVM-based applications to natively executable binaries) — This AOT capability is called a Native Image (the AOT Compiler is introduced in Java 9).
- Support for multiple languages (makes it possible to run JavaScript, Ruby, Python, and a number of other popular languages on the JVM)
👍🏻 Better performance with less memory
👍🏻 Produces less garbage
👍🏻 Decrease in application startup times
👎🏻 Long build times
- Its package manager, called gu, lets users install other languages.
- There are two editions: Community and Enterprise Edition
GraalVM Native Image
- Compiles Java code ahead-of-time into a native executable file.
- Only the code that is required at run time by the application (a fraction of the resources required by the JVM) is included in the executable file.
- While the AOT compiler compiles Java code into a native executable, it will analyze its dependencies, dependent JDK libraries and VM components and removes all unused classes, method, and fields from your application and libraries.
- This process makes reverse engineering difficult by converting Java bytecode into native machine code. Potential attack surfaces are also minimized.
- Supported by many microservice frameworks: Micronaut, Spring, Helidon, Quarkus, etc.
- Maven and Gradle plugins for Native Image so you can easily build, test (there is also JUnit testing support), and run Java applications as executable files.
👍🏻 Costs less to run
👍🏻 Fast startup, no warmup
👍🏻 Ideal for containers (can be packaged into a lightweight container image for faster and more efficient deployment)
Here is a performance comparison based on execution time and RSS, taken from the official site. Resident set size (RSS) is the portion of memory occupied by a process that is held in main memory (RAM).
Happy Coding!
References:
https://www.theserverside.com/definition/GraalVM
https://www.graalvm.org/
https://medium.com/trendyol-tech/native-spring-boot-applications-with-graalvm-part-1-introduction-6fb71606707f
https://medium.com/trendyol-tech/native-spring-boot-applications-with-graalvm-part-2-build-native-image-performance-results-4badfa16b41b
https://www.graalvm.org/java/advantages/#accelerating-java-performance
https://www.graalvm.org/native-image/
https://docs.oracle.com/en/learn/graalvm-and-containerisation/index.html#introduction
https://en.wikipedia.org/wiki/Resident_set_size
https://spring.io/blog/2022/03/23/building-native-images-with-graalvm-and-spring-native-on-apple-s-m1-architecture
https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/
https://www.infoq.com/articles/native-java-graalvm/
https://www.baeldung.com/ahead-of-time-compilation