Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Natively compile Java code for better startup time

July 30, 2018
Faisal Masood
Related topics:
ContainersJavaServerless
Related products:
Red Hat build of OpenJDKRed Hat build of QuarkusRed Hat JBoss Enterprise Application Platform

Share:

    Microservices and serverless architectures are being implemented, or are a part of the roadmap, in most modern solution stacks. Given that Java is still the dominant language for business applications, the need for reducing the startup time for Java is becoming more important. Serverless architectures are one such area that needs faster startup times, and applications hosted on container platforms such as Red Hat Openshift can benefit from both fast Java startup time and a smaller Docker image size.

    Let’s see how GraalVM can be beneficial for Java-based programs in terms of speed and size improvements. Surely, these gains are not bound to containers or serverless architectures and can be applied to a variety of use cases.

    GraalVM allows you to compile a program ahead of time into a native executable. The resulting program does not run on the Java HotSpot VM, but instead uses necessary components such as memory management and thread scheduling from a different implementation of a virtual machine called Substrate VM. Substrate VM is written in Java and compiled into the native executable. The resulting program has faster startup time and less runtime memory overhead compared to a Java VM. (This definition is referenced from the Graal website).

    This article compares the following metrics:

    • JVM startup time with GraalVM RC4 versus OpenJDK 10
    • The bare minimum Docker image sizes needed for running the uberjar versus the native binary

    The complete code for this exercise is available in this repository. The Netty-related part of this repository is made available by Codrut Stancu via his excellent blog.

    This project uses the following tools:

    • Graal
    • Netty
    • Mongo Reactive Streams
    • Project Reactor
    • Vegeta

    Let’s first compare the startup time.

    Startup time

    The startup time is measured as the time difference between the program start and when the program is ready to take requests. It is very encouraging to see that the native compiled image brings us an average startup time of ~17 milliseconds compared to an average of ~850 milliseconds with the JVM.

    This improvement warrants using the Graal native image for applications that need fast startup times, for example, serverless architectures and containers. However, the native compilation has some specific restrictions, especially around the use of reflection and dynamic class loading. This makes it harder (at least for now) to move all applications to native binaries, but with every release of Graal, compatibility is improving.

    The other notable metric is that the size of the native image is almost double the size of the uberjar. The uberjar is about 7.0MB while the native binary is approximately 15MB. Keep in mind that the uberjar needs the Java runtime to be operational, as you shall see in the "Container size" section below, while with the native image, the size of the Docker container is smaller than that with the uberjar container.

    JVM Startup times in milliseconds. Smaller is better.

    JVM startup time comparison">

    Setup

    If you want to run this application locally, make sure that the MONGO_IP (which defines the IP address of the MongoDB database) and DIE environment variables are set. If DIE is set to any value, this application will terminate as soon as it starts completely. To run the application, you need to unset this variable. If the DIE variable is unset, the application will start and take requests. The application can be accessed via http://localhost:8080/.

    To run the application locally, do the following:

    $ git clone https://github.com/masoodfaisal/need4speed.git
    $ cd netty-mongo-native
    $ mvn clean install
    $ export MONGO_IP=127.0.0.1
    $ export DIE=true #or unset DIE if you want the application to stay alive
    $ time java -jar target/netty-mongo-native-full.jar

    The full binary of the application is made available via the following command. Make sure that Graal is available on your local machine.

    native-image -jar target/netty-mongo-native-full.jar \ 
    -H:ReflectionConfigurationResources=netty_reflection_config.json \ 
    -H:Name=netty-svm-http-server \ 
    -H:+ReportUnsupportedElementsAtRuntime
    

    The startup time is measured with the new JVM compiler interface (JVMCI) options available from JDK 9. The measured time reported here is recorded with the following JVM flags:

    -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCI

    Note that no measurements have been captured via ahead-of-time compilation (AOTC), and it would be interesting to see what changes AOTC brings to the mix.

    Container size

    The Docker image for the natively compiled artifact is less than half the size of the container with the JRE. Native compilation provides great savings in the Docker image size, and this could be a big win for container environments such as Kubernetes.

    Docker container size in M. Smaller is better

    Container size comparison">

    Setup

    The fedora-minimal base image is being used as the runtime for the JVM and native images. The sizes shown in the metrics above are the from SIZE column of the docker images command.

    All Docker files are available in the source repository. Build the images using following commands:

    $ docker build -t fedora-minimal:jvm --file DockerfileFedoraMinimalJVM .
    $ docker build -t fedora-minimal:native --file DockerfileFedoraMinimalNative .
    $ docker build -t fedora:jvm --file DockerfileFedoraJVM .
    $ docker images

    Application response times

    Response times indicate the median response time for each execution. This exercise is not intended to measure the application performance, and the code provided in the repo is not optimized for performance. Instead, the focus is on finding whether native compilation introduces any overhead to the average response times. From the numbers below, it can be seen that the native image is not adding any specific overhead to the overall performance. This test was executed for less than a minute and as Hotspot comes into play, the JDK response times probably would start to more closely match the native response time.

    Median Response time in milliseconds

    Response times comparison">

    Setup

    Vegeta, an HTTP load testing tool, is used to put a load on the application for 60 seconds.

    The test used vegeta with the following parameters:

    $ echo "GET http://localhost:8080/" | \ 
      ./vegeta attack -duration=60s -rate=200 -keepalive=false | \ 
      tee results.bin | \ 
      ./vegeta report

    Everything is running on a laptop with an Intel Core i7 processor and 16GB of RAM. MongoDB is also running locally and no tuning has been performed on any specific parameter.

    Conclusion

    Serverless architectures can benefit from faster startup times. The configuration demonstrated in this article shows how GraalVM can reduce startup time and Docker image size for Java-based programs.

    For your Java applications that are hosted on container platforms such as Red Hat OpenShift, try natively compiling them using GraalVM and its AOTC capability to see how they can benefit from faster Java startup time and a smaller Docker image size.

    Last updated: February 11, 2024

    Recent Posts

    • How Trilio secures OpenShift virtual machines and containers

    • How to implement observability with Node.js and Llama Stack

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    • Speech-to-text with Whisper and Red Hat AI Inference Server

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue