Getting started with native java apps with GraalVM

saad elattar

Elattar Saad

Wed Apr 10 2024

3 min read7

What is native image?

Native Image is a technology to ahead-of-time compile Java code to a standalone executable, called a native image. This executable includes the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK. It does not run on the Java VM, but includes necessary components like memory management, thread scheduling, and so on from a different runtime system, called “Substrate VM”. Substrate VM is the name for the runtime components (like the deoptimizer, garbage collector, thread scheduling etc.). The resulting program has faster startup time and lower runtime memory overhead compared to a JVM. — Oracle

What is GraalVM?

GraalVM is a Java Development Kit (JDK) written in Java. The open-source distribution of GraalVM is based on OpenJDK, and the enterprise distribution is based on Oracle JDK. As well as just-in-time (JIT) compilation, GraalVM can compile a Java application ahead of time. This allows for faster initialization, greater runtime performance, and decreased resource consumption, but the resulting executable can only run on the platform it was compiled for.

JIT vs AOT compilations

What is just-in-time compilation?

With just-in-time compilation, portions of the program are dynamically compiled into machine code by the Java Virtual Machine (JVM) while it is executing and analyzing the application's bytecode. JIT compilation enhances performance by optimizing code segments that are often run.

When to use JIT?

What is ahead-of-time compilation?

In contrast, the process of converting source code into machine code prior to the program being executed is known as "ahead-of-time compilation." AOT compilation involves compiling the program in advance, usually during the build step, or at least all of its major components. There is no need for further compilation during runtime because the generated binary is platform-specific.

When to use AOT?

Since the difference between the two types are clear, the following sections will be focused on the use of AOT and its cons.

AOT is great but what’s the catch?

Dynamic Class Loading

Any class that has to be known at executable construction time in order to be accessible by name at executable runtime. For instance, myClass has to be in a configuration file in order to be called using Class.forName("myClass"). A ClassNotFoundException will be issued at runtime, as if the class was not found on the class path or was inaccessible, if a configuration file is provided but does not contain a class that is necessary for dynamic class loading.

Reflection

The native-image tool has to be aware of all the classes, methods, and fields that should be available through reflection at build time. In an attempt to resolve these software pieces, Native Image looks for calls to the Reflection API using static analysis. The program parts that are reflectively accessible at runtime must be defined at build time in a configuration file or by using RuntimeReflection from a Feature in the event that the analysis is unsuccessful.

Java Native Interface (JNI)

In the same manner that Java code uses the reflection API, native code may access Java objects, classes, methods, and fields by name. Java assets that may be accessed by name using JNI need to be listed in a configuration file provided to the native-image tool during build time.

Serialization

Similar to JNI, class metadata information is needed for Java serialization, and it has to be supplied in a configuration file to the native-image tool during build time. Still, security flaws in Java serialization have been around for a while. The current serialization system will soon be replaced with a new one that avoids these issues, according to an announcement made by the Java architects.

More limitations and differences are discussed here.

Read next

Blossoming Intelligence: How to Run Spring AI Locally with Ollama

In this short article, we'll look at how easy it is to create a chat bot backend powered by Spring and Olama using the llama 3 model.

Sat May 11 2024

Read More

React 19: The long-expected features

React 19 introduces a number of new features that will undoubtedly make the life of react developers simpler. While the release is not yet stable, you can test out the new features using the canary version.

Wed Apr 17 2024

Read More

Leveraging Spring Reactive, Functional Endpoints, Docker, and MongoDB

Blocking is a feature of classic servlet-based web frameworks like Spring MVC. Introduced in Spring 5, Spring WebFlux is a reactive framework that operates on servers like Netty and is completely non-blocking. Two programming paradigms are supported by Spring WebFlux. Annotations (Aspect Oriented Programming) and WebFlux.fn (Functional Programming).

Thu Feb 29 2024

Read More

NextJs meets Redux: A simple user PoC

Redux is a powerful state management library primarily used in JavaScript applications, particularly those built with frameworks like React. At its core, Redux provides a predictable state container for managing the state of an application in a more organised and centralised manner. It operates on a unidirectional data flow model, which helps in maintaining the consistency of application state and facilitates easier debugging and testing.

Thu Feb 15 2024

Read More
Saad Elattar © 2024
|