Java Microframeworks with GraalVM

Mani BalaMani Bala
6 min read

Java GraalVM is considered a game changer in the Java world because it provides a number of benefits that were previously not available with traditional Java implementation. Microframeworks, one could be curious. it is a term used to refer to minimalistic web frameworks to build microservice applications such as Quarkus, Micronaut, Helidon, etc.

This article aims to explain how enterprise architects, developers, and managers can extend or migrate an existing SpringBoot tech stack using lean Java frameworks. Let's also discuss some runtime and detailed metrics of those applications.

An Introduction to Java GraalVM

Improving Java Performance with Graal Compiler, If you're a Java developer looking for ways to improve the performance of your applications, you might want to consider using Java GraalVM. This high-performance virtual machine is designed to run Java code efficiently on a variety of platforms.

We note that Microservices have established themselves as the de facto architecture standard for corporate software development. At the same time, Spring Boot emerged as the most well-liked and commonly utilized framework for Java-based microservice implementation. Spring Boot has occupied the top spot for many years.

Recently, RedHat developed a Quarkus framework while OCI developed the Micronaut framework. But We are gonna see the demo of graalVM performance with Java counter program example on below.

What is Java GraalVM?

Java GraalVM is a virtual machine that is built on top of the Graal compiler, a state-of-the-art just-in-time (JIT) compiler that can generate highly optimized native code. This makes GraalVM much faster than traditional JVMs, especially when it comes to executing complex code.

GraalVM allows users to write polyglot applications that seamlessly pass values from one language to another. Since Graal supports a polyglot virtualization layer, you can run Java, Scala, Koltlin, Javascript, Ruby, and Python.

How does Java GraalVM improve Java performance?

One of the ways that Java GraalVM improves Java performance is through its support for ahead-of-time (AOT) compilation. With AOT compilation, you can compile your Java code into a native executable that can be run directly on your target platform, without the need for a separate JVM. This can help you reduce startup times and improve overall application performance.

Getting started with Java GraalVM:

If you're interested in using Java GraalVM, There are a few steps you can take to get started:

  1. Download Java GraalVM.

  2. Update your PATH environment variable to point to the GraalVM bin directory.

  3. Configure your IDE to use the GraalVM compiler and runtime.

  4. Test your code to ensure that it's running correctly and efficiently.

By following these steps and taking the time to learn about the advanced optimization techniques and features provided by Java GraalVM, you can improve the performance of your Java applications and take your development skills to the next level.

Experimental:

To demonstrate performance gains when using the Graal compiler. This example counts the number of uppercase characters in a body of text.

public class Main {
    static final int ITERATIONS = Math.max(Integer
                                          .getInteger("iterations", 1), 1);
    public static void main(String[] args) {
        String sentence = String.join(" ", args);
        for (int iter = 0; iter < ITERATIONS; iter++) {
            if (ITERATIONS != 1) 
                System.out.println("-- iteration " + (iter + 1) + " --");
            long total = 0, start = System.currentTimeMillis();
            last = start;
            for (int i = 1; i < 10_000_000; i++) {
                total += sentence.chars()
                                .filter(Character::isUpperCase)
                                .count();
                if (i % 1_000_000 == 0) {
                    long now = System.currentTimeMillis();
                    System.out
                        .printf("%d (%d ms)%n", i / 1_000_000, now - last);
                    last = now;
                }
            }
            System.out.printf("total: %d (%d ms)%n", 
                                total, System.currentTimeMillis() - start);
        }
    }
}

Now, Running the simple Java program with the different run configurations shown below,

I'm using one Loader configuration with the default JDK-17. another Loader using GraalVM-17 was also employed.

We can now clearly view the differences by running the two different application and output's are shown below,

Standard JDK


GraalVM

Quarkus vs Micronaut:

The real Java world game changer. Two popular options that have gained significant traction in recent years are Quarkus and Micronaut. Both frameworks offer lightweight and cloud-native capabilities.

  1. Lightweight and Fast:

    Both Quarkus and Micronaut excel in providing lightweight and fast application development experiences.

    Quarkus leverages a combination of GraalVM (is an universal virual machine) and an ahead-of-time (AOT) compilation.


    Micronaut follows a similar approach by using compile-time dependency injection and AOT compilation, resulting in small memory footprints and blazing-fast startup times.

  2. Developer Experience:

    When it comes to developer experience, both frameworks offer a robust set of tools and features.

    Quarkus provides a seamless development experience with its DevMode, allowing for hot-reloading and a broad range of supported extensions, making it easier to integrate with other technologies


    Micronaut also offers a great developer experience with features like automatic configuration, empowering developers to focus on writing code rather than dealing with boilerplate configurations.

  3. Cloud-Native Capabilities:

    When it comes to developer experience, both frameworks offer a robust set of tools and features.

    Quarkus, with its focus on optimizing for serverless and container-based deployments, provides native support for popular cloud platforms like Kubernetes and OpenShift. It offers features like efficient memory management, reactive programming, enabling developers to build scalable and resilient applications


    Micronaut, too, embraces the cloud-native paradigm by offering built-in support for cloud services and serverless computing. It provides seamless integration with platforms like AWS Lambda, Google Cloud Functions, and Microsoft Azure Functions.

  4. Community and Ecosystem:

    The strength of a framework often lies in its community support and ecosystem.

    Quarkus has gained substantial popularity and has a thriving community, which translates into excellent documentation, tutorials, and a wide range of community-driven extensions. It benefits from its association with Red Hat .


    Micronaut, while relatively newer, has also attracted a committedcommunity and is actively evolving. It offers a growing ecosystem of plugins, libraries, and frameworks that extend its capabilities, enabling developers to leverage existing tools and components.

Resultant Metrics:

When considering the performance of Java frameworks like Quarkus and Micronaut, one crucial aspect to examine is the use of native images. Native images, generated through ahead-of-time (AOT) compilation, offer improved startup times and reduced memory consumption compared to traditional Just-In-Time (JIT) compilation.

The results of using Native images: Memory footprints


The results of using Native images: Startup time

Java framework evaluation:

Conclusion:

Choosing between Quarkus and Micronaut depends on your project requirements and priorities. Quarkus is an excellent choice for developers looking for maximum performance, scalability, and cloud-native capabilities. Its extensive ecosystem and community support make it suitable for complex enterprise applications. On the other hand, Micronaut is a solid option if you value developer experience, fast startup times, and low memory footprints. Its seamless integration with cloud services and its focus on serverless architectures make it ideal for microservices and cloud-native applications.

Whichever framework you choose, both Quarkus and Micronaut offer compelling features that enable modern and efficient Java application development.

Happy Learning...!

Additionally, I found these hot frameworks. It could be useful.

22
Subscribe to my newsletter

Read articles from Mani Bala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mani Bala
Mani Bala

Let's innovate, learn, and build amazing solutions together!