Home

Awesome

Libgdx + Graalvm native-image example

logo

Introduction

Please check my video for short introduction: https://www.youtube.com/watch?v=9eevmylv0fU&ab_channel=ByerNiX

Why?

I was curious how much Graalvm native-image could help with my java game. Main things that could be improved was:

How?

GraalVM uses SubstrateVM under the hood for native-image generation. Native image is generated as AOT (ahead of time) compiled java. There is no JIT compiler, native-image has to be compiled on the system for which is created, result is one executable file with JRE classes compiled inside. Let's go through the process.

Setup

Install GraalVM, set it as default java in environment variables, make sure you use windows x64 native tools when running GraalVM native-image on Windows (other systems were not tested in scope of this project). I've used this link when doing it first time.

Next, clone this repo, make fat-jar as normal (desktop dist). Put your jar in the <b>graalvm-env</b> directory and run <b>build_native.bat</b>

What do we have here?

<b>desktop</b> and <b>core</b> contains sample code covering:

<b>libgdx-native-image</b> contains substitutions for Kryo, Libgdx nad Lwjgl. <br> <b>graalvm-env</b> contains GraalVM config files, assets and libs ready to run and test result application.

Troubleshooting

Native image is a little tricky to work with. There are some limitations (mainly reflections and jni) that has to be somehow addressed.

Tips and tricks:

Is it worth?

I don't think so. I've compiled my own game TAnima:

tanima

to native image and I don't see any significant boost. I'd say that same app on jvm16 (graalvm is only java11 right now) performs even better. Comparing java11 with graalvm native image- native is much more stable, takes a little less memory and less GC pauses.

Disadvantages