Introduction to GraalVM
What is GraalVM?, Why GraalVM?, What is “GraalVM Native Image”?
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.
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.
- 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).
👍🏻 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).