Stress-testing native image and Quarkus Desktop ... by building silly games
Recently, I’ve been experimenting with putting the “image” into “native image”. What if every pixel was a process?
The answer? Well, it works, but you’d better choose your stack carefully. Java still has a reputation for heaviness in some circles, but if you’ve been paying attention to what’s going on with GraalVM native image over the past few years, you’ll know that Java can be very light indeed.
How light? I decided to make a demo to showcase the start times of GraalVM applications in a visual way, while also showing off the new quarkus-desktop extension (introduced here). My first iteration was Quark-ified whack-a-mole. A controller launches a dozen or so windows, one of which has a mole. Click the window to dismiss the mole, and a new one pops up to replace it. It feels instantaneous. Under the covers, each window is its own Quarkus application. With a dozen applications, my laptop doesn’t even notice the load.
To demonstrate Quarkus’s networking APIs, I added inter-process communication, so the windows dodge the mouse and then use WebSockets to prompt their immediate neighbours to also shuffle over. It makes the game a little more challenging, and it’s fun.
Can we go further? Yes! What if moles just keep coming? I call this variation Mole Apocalypse. I used a Quarkus profile to capture the more aggressive mole configuration. As soon as a mole has been started and painted, the next one is launched. Whack-a-mole was pretty easy to play, but Mole Apocalypse is almost impossible. On my Mac, the screen fills up within about a second. Using JVM moles instead of native moles tilts the balance, and keeping on top of the incoming moles becomes pretty easy. Leyden is somewhere in the middle.
Mole Apocalypse is a good test of human reflexes, but even with dozens of application windows appearing and disappearing, my laptop is barely breaking a sweat. Can we go even further? Yup. Inspired by Max Andersen’s TamboCam (built with TamboUI), I decided to make a MoleCam. Every pixel is a process.
I added another Quarkus profile which rendered the windows without a border, wired in a camera input to the controller, and updated the controller to dynamically update the window colours. I tested it out with 500 processes. Windows started appearing in a neat tiled formation, I could see video, and then … my whole screen went black. My Mac had crashed.
Inspecting the logs, it turns out I’d caused a null dereference somewhere in the GPU libraries, which then triggered a system panic. The crash diagnostics listed 49 swap files, so the machine had been paging frantically before it went down. Oops. I tried again with a more modest 256 processes, and … CRASH. Another GPU null pointer. This wasn’t a problem in my code, and it probably isn’t even a fault in the JVM or GraalVM. I’d just consumed so much memory, so quickly, while also driving the GPU really hard, and somewhere something important got evicted from the GPU’s memory.
I tried with 64 processes, and my mac didn’t crash. That was something, but … it’s not much. At this point, I was feeling worried for my demo, because a 64-pixel image isn’t particularly impressive to look at. It’s barely even recognisable as an image. I tried playing around with launching the processes in waves, or delaying the video until everything was up, but nothing helped. Every test took out my whole machine, which is an annoying way of doing software development.
The problem was that every mole process was consuming a huge amount of memory. What was going on? Quarkus native images are supposed to be light, but mine seemed to need more than 200 MB each. Even my paltry 64 moles were using about 13 GB of RAM. And when I started streaming video, the memory requirements ramped up. On investigation, it turns out each AWT mole sets up its own Metal pipeline, and on every re-paint, the pipelines grabbed more memory. Most of the memory being consumed was system memory, not application memory – those windows were putting a heavy load on my operating system, even though the Java part was light.
Why was I using AWT for graphics?
The quarkus-desktop extension has modules for -swt, -awt, and -awt-swing.
I’d initially implemented whack-a-mole with SWT, because I like its OS-native widgets, but the GraalVM-native first-paint times were much longer with SWT.
Every SWT process had to load the SWT native library, create a Display and run an AppKit event loop.
So I switched to AWT to bring the reported paint times down. That worked well.
But for MoleCam, having a fast paint time on one tile was no use if the whole machine kept crashing.
I switched to SWT, and the system memory per mole went from 206 MB to 24 MB.
That’s about 9x less! Even better, GPU utilisation for my test batch went from 76% to 1%.
CPU utilisation went from 49% to 14%. SWT was more efficient in every metric but start time – but even for start time, at scale, the SWT moles stressed the system so much less that the aggregate start time ended up being about the same as for AWT.
For something like whack-a-mole, where I was rendering text and images and system windows, a graphics library such as AWT or SWT was clearly justified. For MoleCam, all I needed each window to do was show a single colour. Did I even need a graphics toolkit? I repeated my experiments, using direct FFM calls to the Apple APIs. Memory dropped even further, and the aggregate start time halved. My borderless SWT windows didn’t have shadows, which is partly why they barely touched the GPU. With FFM, I could render both with and without shadows. The shadows didn’t affect memory, but they did take GPU from 0% to 49%. With so many tightly-packed windows, all changing colour, keeping up with the shadow calculations is hard work for the GPU.
The video is a bit pixelated, but it’s recognisably(ish) a video of me. I’ve managed to render a video using 900 independent Java processes as pixels. Should you try to do something similar? Absolutely not, it’s a silly thing to do. But, if you do want to optimise footprint for your Quarkus Desktop application, I recommend SWT. If startup time is most important and you’re running on a well-resourced machine, go AWT. And if you really want to squeeze every last bit of performance, consider FFM, which makes calling native APIs from Java a lot easier than it used to be.
To confirm the difference, I did some simple benchmarking on my macOS laptop. I measured 64 native moles, each streaming 20 seconds of ffmpeg test video, on an Apple Silicon Mac with 52 GB of memory (the ranges are where I did several runs):
| AWT | SWT | FFM | FFM + shadow | |
|---|---|---|---|---|
Window shadow |
yes |
probably not |
no |
yes |
Moles up in |
4.2 s |
4.4 s |
2.3–3.2 s |
2.5–2.6 s |
Memory per mole (system) |
206 MB |
24 MB |
13–21 MB |
15–17 MB |
Mole footprint |
230 MB |
18 MB |
15 MB |
15 MB |
Mole RSS |
120 MB |
98 MB |
83 MB |
83 MB |
GPU memory |
+10.9 GB |
+0.1 GB |
about 0 |
+0.2 GB |
GPU busy |
76% |
1% |
0% |
43–44% |
Window server CPU |
46% |
15% |
15–17% |
27–31% |
Moles' CPU, all 64 |
49% |
14% |
17–18% |
14–15% |
Fine print: these figures come from the CompareToolkits script in the quarkus-moles repository, which ran MoleCam with 64 native moles tiled across an assumed 1920×1080 screen, using a test video rather than a webcam, for 20 seconds after the moles had come up and settled. The colour updates ran at up to 30 frames a second for SWT and FFM, but at a lower default rate for AWT, so AWT’s numbers are not strictly like-for-like. Memory per mole is the drop in available system memory divided by the number of moles, GPU memory is system-wide so other applications can shift it by a few hundred MB, and several of the figures are ranges because I ran them more than once.
Nearly all of an AWT mole’s cost is GPU memory, because each one sets up its own Metal pipeline. Shadows cost GPU time and WindowServer CPU, but little memory. Once I realised the advantage SWT had over AWT, I went back and revisited my design choices for Mole Apocalypse. I’d decided to use AWT based on measurement of one mole, but it turns out that at scale, the answer was different. SWT moles fill the screen a third faster than AWT moles. This is the classic benchmarking error. In order to make good decisions, you need to measure under production-like conditions. "Production" maybe isn’t the right word for Mole Apocalypse, since I suspect this is an application that will never run in production, but the principle is the same – measure the things you actually care about, not a cut-down version that’s had the realism squeezed out of it.
Back to FFM. As you can see from the table, FFM was even leaner and faster than SWT. The disadvantage is that the bindings need to be hand-maintained for each operating system. That’s hard work and fragile, so I opted not to attempt an FFM version of Mole Apocalypse. But in the context of MoleCam, the complexity trade-off of FFM was worth it. My mole-ceiling jumped from around 500 moles with SWT to around 1000 with FFM. In fact, I launched so many moles that I hit a different ceiling. macOS won’t allow more than 1020 windows. Most systems usually have quite a few pre-existing windows hanging around, and I’m atrocious at closing tabs, so I have more than most. My moles capped out around 900. Beyond that, the OS refused to participate, and my mole-launches just didn’t get a window.
The demo code for whack-a-mole, Mole Apocalypse, and MoleCam is in the quarkus-moles repository.
Postscript: What about Linux?
All of these measurements and trade-offs are for Mac, but I did also do some measurements on Linux. As you would expect, FFM is cheapest, but the different operating system changed the AWT and SWT stories. On my Linux system, AWT was both faster and leaner than SWT. However, there is a big caveat. I was running headless, with an Xvfb virtual windowing system. When I added an Openbox window manager, the advantage flipped again, and SWT was faster than AWT, and used around 4x less memory. And even Openbox on Xvfb isn’t a real UI. So the short answer for Linux is: I don’t know!
Post-postscript: What about Windows?
I attempted Linux, but without the hardware needed to make a proper assessment. I didn’t even attempt Windows measurements; if you’re evaluating Quarkus desktop options on Windows, I encourage you to do your own measurements.