Native Executables

Native Executables

Native executables, created through ahead-of-time (AOT) compilation, are standalone binaries that can be executed directly on a target operating system and architecture, unlike traditional Java applications which require a Java Virtual Machine (JVM). GraalVM Native Image is a technology that allows Java code to be compiled into native executables, offering benefits such as faster startup times, reduced memory footprint, and improved resource efficiency, especially in containerized and cloud environments.

Key characteristics of native executables:

  1. Ahead-of-time (AOT) compilation: Native Image compiles Java code into machine code before runtime, eliminating the need for a JVM to interpret or JIT-compile the code at runtime.
  2. Standalone binaries: Native executables are self-contained and do not require a separate JVM installation.
  3. Optimized for specific platforms: Native Image generates executables tailored to the target operating system and architecture, leading to optimized performance.
  4. Smaller memory footprint and faster startup: By including only the necessary code, native executables have a smaller memory footprint and start up much faster than their JVM-based counterparts.

Benefits of using native executables

  1. Reduced startup time: Significantly faster startup times make them ideal for applications that need to scale quickly or are frequently started and stopped.
  2. Lower memory consumption: Native executables consume less memory, making them suitable for resource-constrained environments like containers and cloud platforms.
  3. Improved resource efficiency: They require less CPU and memory, leading to lower operational costs in cloud deployments.
  4. Enhanced security: Native executables can be deployed in distroless or even scratch container images, reducing the attack surface and improving security.

How to create native executables

  1. GraalVM Native Image: GraalVM Native Image is a tool that allows you to compile Java applications into native executables. It can be integrated with build tools like Maven and Gradle.
  2. Spring Native and Quarkus: Frameworks like Spring and Quarkus offer built-in support for creating native executables using GraalVM Native Image, simplifying the process.
  3. Native Build Tools: Native Build Tools provide Maven and Gradle plugins that automate the process of building native images.
  4. Static and mostly-static linking: Native Image allows you to create statically linked or mostly-statically linked executables, providing options for different deployment scenarios.

Example

A Spring Boot application can be packaged into a native executable using Spring Native and Native Build Tools. This results in a smaller, faster, and more efficient application that can be deployed in containers

TODO

  1. https://www.graalvm.org/latest/reference-manual/native-image/guides/containerise-native-executable-and-run-in-docker-container/
  2. https://www.graalvm.org/22.2/reference-manual/native-image/guides/containerise-native-executable-and-run-in-docker-container/
  3. https://spring.io/blog/2023/09/09/all-together-now-spring-boot-3-2-graalvm-native-images-java-21-and-virtual
  4. https://docs.spring.io/spring-boot/reference/packaging/native-image/index.html
  5. https://www.baeldung.com/spring-native-intro
  6. https://github.com/graalvm/graalvm-demos/tree/master/native-image/spring-boot-microservice-jibber
  7. https://www.reddit.com/r/java/comments/10cv886/personal_experiences_with_native_graalvm_images/

Links to this note