Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala
Tags: agilewebandappdevelo
Published: 2021-06-02 08:44:06

---
title: Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala
published: true
date: 2021-05-27 11:56:00 UTC
tags: Agilewebandappdevelo
canonical_url: https://apiumhub.com/tech-blog-barcelona/quarkus-graalvm-java-kotlin-scala/
---

Due to the actual microservices server paradigm & server costs corresponding to resource’s consumption, [Quarkus](https://quarkus.io/) is a framework that caught my attention some years ago, as it solves JVM language problems on these concerns. In this article you will find Quarkus GraalVM examples and explanation. We are going to create a basic Quarkus REST-API application & create a native image with GraalVM.  Also we’ll mention some existing alternatives to Quarkus.

## Quarkus GraalVM

The generated Quarkus project for this article can be found in our [GitHub repository](https://github.com/davidgfolchApium/quarkus-basic.git).

### GraalVM

GraalVM is a universal virtual machine for running applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin, Clojure, and LLVM-based languages such as C and C++.

### Quarkus project

This framework is designed to:

- minimize the size of the generated jar files.
- lower memory usage.
- fastest application startup.

![Screenshot 2021 05 28 at 13.40.51](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.40.51.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 1")

Some metrics from the official site:

![Screenshot 2021 05 28 at 13.41.44](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.41.44.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 2")

All these improvements can be accomplished [thanks to the following](https://quarkus.io/vision/container-first):

- Use of GraalVM: native image build
- Build Time Metadata Processing: removes unused classes
- Reduction in reflection usage
- Native Image Pre Boot: preboot image as much as possible at build time serializing the state in the executable.

Note that for java projects jdk8 is supported but jdk11 is the [default from version 1.13](https://quarkus.io/blog/quarkus-1-3-1-final-released/).  Also Quarkus embraces the [https://12factor.net/](https://12factor.net/) witch is basic in modern software development.  
  
LICENCE: under the[Apache Software License 2.0](https://www.apache.org/licenses/LICENSE-2.0) or compatible license.

#### Documentation & guides

The documentation in the official Quarkus page is complete enough and with lots of [guides](https://quarkus.io/guides/) for specific implementations & integrations.  Also checkout the official  [Quarkus github repository](https://github.com/quarkusio/quarkus) and the [wiki](https://github.com/quarkusio/quarkus/wiki).

##### Bootstrapping a project

We have several options to start a new project:

- Using [https://code.quarkus.io/](https://code.quarkus.io/) (similar to Spring initializer) to bootstrap your project dependencies & archetype.  Some extensions are in preview or experimental state, so be careful.
- With official quarkus [maven archetypes in github](https://github.com/quarkusio/quarkus-quickstarts).
- Via command line with the maven (or gradle) [plugin](https://quarkus.io/guides/maven-tooling).
- With IDE Quarkus plugins (f.ex. for [Intellij](https://www.jetbrains.com/help/idea/quarkus.html) is already bundled).

##### Simple example

We are going to start with a simple example on how to create, build & run a project with the [starter web-site](https://code.quarkus.io/).  First we select some packages and download the project as a zip:

![Screenshot 2021 05 28 at 13.44.53](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.44.53.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 3")

Quarkus starter web-site helps you with some info on how to proceed:

![Screenshot 2021 05 28 at 13.45.54](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.45.54.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 4")

Now we are going to move it to a proper folder:

```
mkdir ~/projects/articles
mv ~/Downloads/quarkus-basic ~/projects/articles/quarkus-basic
cd ~/projects/articles/quarkus-basic
```

You can now import the project into your IDE.  Checkout the **README.md** & some **code examples generated** by the initializer page.

**Build & run the project**

Be sure you use jdk11:

```
# With sdk-man (use or install):
sdk use java 11.0.10.hs-adpt
# Build & run with maven
./mvnw compile quarkus:dev

```

This should show you some logging in the terminal:

![Screenshot 2021 05 28 at 13.48.29](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.48.29.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 5")

Checkout the [Dev UI](http://localhost:8080/q/dev/) & the application [end-point](http://localhost:8080/).

![Screenshot 2021 05 28 at 13.49.06](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.49.06.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 6")

### Build native image with GraalVM docker image

We can use maven-quarkus-plugin to use a dockerized GraalVM to create a native image of our application.

NOTE: On MacOS11.3 with docker version (20.10.5) it didn’t work,  I was getting an **out of memory error** when packaging to native. I tried to set quarkus.native.native-image-xmx=6g  in the application.properties to set some extra memory for the Java heap (see [docs](https://quarkus.io/guides/building-native-image#quarkus-native-pkg-native-config_quarkus.native.native-image-xmx)), but it didn’t work either. It works out-of-the-box in an Ubuntu 20.04.2 LTS.

And now execute next in terminal to generate the native image:

```
./mvnw package -Pnative -Dquarkus.native.container-build=true

```

It will take about 3 minutes to the _quarkus-maven-plugin_ to package our small project:

![Screenshot 2021 05 28 at 13.50.33](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.50.33.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 7")

And the memory (& cpu) usage is quite big:

![Screenshot 2021 05 28 at 13.51.08](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.51.08.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 8")

As we can see, the generated runner’s at the target folder is quite small compared to Spring generated jars:

![Screenshot 2021 05 28 at 13.51.33](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.51.33.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 9")

You can execute the native application from your terminal:

```
./target/quarkus-basic-1.0.0-SNAPSHOT-runner

```

As you can see in the image below it starts really quickly (less than 1 second the first time & 0.030s second time):

![Screenshot 2021 05 28 at 13.52.24](https://apiumhub.com/wp-content/uploads/2021/05/Screenshot-2021-05-28-at-13.52.24.png "Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala 10")

#### Conclusion

**Quarkus + GraalVM** is great for small microservices projects for all performance improvements it brings into the scene, but these improvements decrease as much as project size increases.

Also to have in mind that the performance on Quarkus applications could be affected by the use of some Quarkus libraries that need some performance/resource improvements.

Current alternatives are:

- [Helidon Microprofile](https://helidon.io).
- [Spring Native (beta).](https://github.com/spring-projects-experimental/spring-native)
- [Micronaut Framework](https://micronaut.io/).

#### Links

Quarkus official:

- [Quarkus project github](https://github.com/quarkusio/quarkus).
- [Quarkus application initializer web-site](https://code.quarkus.io/).
- [Check out the Quarkus guides!!](https://quarkus.io/guides/)
- [Quarkus app seeds maven artifacts repository](https://github.com/quarkusio/quarkus-quickstarts).

Further reading:

- [InfoQ: Starting Fast: Investigating Java’s Static Compilation Landscape](https://www.infoq.com/presentations/java-start-fast-graal-substrate/?utm_source=email&utm_medium=java&utm_campaign=newsletter&utm_content=04132021).
- [DZone: AOT compilation](https://dzone.com/articles/complication-compilers-and-java).
- [DZone: Quarkus vs Spring Boot](https://dzone.com/articles/microservices-quarkus-vs-spring-boot).
- [BetterProgramming: Java Microservice Frameworks](https://betterprogramming.pub/which-java-microservice-framework-should-you-choose-in-2020-4e306a478e58).
- [Medium: SpringBoot vs Quarkus: a Real-Life Experiment](https://medium.com/swlh/springboot-vs-quarkus-a-real-life-experiment-be70c021634e#2325).
- [Medium: Faster, Lower, Better with Quarkus in k8s](https://itnext.io/faster-lower-better-with-quarkus-in-k8s-83185af46f36)

If you need any help with this kind of projects, let us know! We are [experts in software development](https://apiumhub.com/)!