Introduction to GraalVM

What is GraalVM?, Why GraalVM?, What is “GraalVM Native Image”?

Photo by Grégoire Bertaud on Unsplash

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
Just In Time Compilation — https://gunceljava.blogspot.com/2018/11/just-in-time-compilation-jit.html

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).

Performance Comparison — https://www.graalvm.org/native-image/
Native Image at Scale — Startup Time — https://www.graalvm.org/native-image/

Happy Coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏼‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨