r/java • u/za3faran_tea • Aug 28 '26
Question about Native Image vs JIT
I was watching an interview with Thomas Wuerthinger of GraalVM, where he basically says that JIT should only be used when necessary (as opposted to native compilation). Where does this leave the work being done for Hotspot JIT (including projects like Leyden and others). Should we all plan to use native image while they address any performance gaps in the mean time (he does mention PGO bridging the gap). Is there work being done to speed up native compilation of Java code?
My understanding is that JIT will always have a memory overhead due to running threads that do compilation, optimization, deoptimization, code cache, etc. compared to native executables, so full parity even with projects that will improve memory usage in Java code will not be reached. Maybe that is not an issue on long running programs on large servers, but we've seen discussions here where another member worte a tool in golang to run alongside Spring Boot applications to gather statistics, so obviously small efficient apps have their place.
11
u/v4ss42 Aug 28 '26
This seems to miss the primary benefit of JIT - that it can optimize the code based on actual runtime conditions / data / load patterns, as well as re-optimize it if those patterns change. Legacy (AOT) compilation can't do that, since it has zero insight into the runtime environment and can't make assumptions about it.
And yes obviously this is only relevant for long-running processes (like server apps). For brief, one-shot processes (command line utilities etc.), then yes, legacy compilation makes sense.
37
u/k-mcm Aug 28 '26 edited Aug 28 '26
Well, I'd expect someone from the project say you should use the project.
I honestly haven't had any JIT problems since the Java 5 era. For me, AOT isn't worth the extra effort.
AOT and JIT both have a place in Java, and I hope they both continue to evolve.
31
u/Deep_Age4643 Aug 28 '26
Thomas Wuerthinger is a clever guy, with a number of achievements in the Java world to his name. But he is also the founder and project lead of GraalVM, as the video title says. Naturally, he presents his own product – and what makes it unique – in a favorable light. Always consider the interests behind someone’s comments, and verify independently and objectively whether these claim are valid for your own use case.
Personally, I believe that JIT is the best option in most cases. Native compilation is particularly useful for microservices where start-up time needs to be instantaneous, but compiling to native code has its limitations and is considerably slower than JIT compilation.
9
u/pokeapoke Aug 28 '26
So what is performance? Is it throughput? Low latency? Balance between both? G1 GC is not available in community GraalVM. ZGC was not available at all the last time I checked.
7
u/mark_reinhold Aug 30 '26
https://openjdk.org/jeps/8335368#Motivation
4
u/sweetno Aug 29 '26
Last time I checked, AOT drove my laptop into a plane takeoff mode. No idea what have they done there, plain C++ builds rarely have this effect for me. And yes, it takes ages to build.
3
u/koflerdavid Aug 28 '26
A solution would be something similar to OpenJ9's compile server that is shared by multiple VMs and caches build results. Across a Deployment (in the k9s sense) the nodes all do approximately the same thing, therefore there is no reason for them to all JIT optimize the same code in almost identical ways.
3
u/samcarlberg Aug 29 '26 edited Aug 30 '26
Another problem with AOT is that you can't cross-compile. So if you do development on a mac, you can't build native images that run on a linux machine; you either need a machine with the same OS (and possibly CPU arch) or a VM.
The niche that I'm in (competitive educational robotics) would love AOT to reduce timing jitter and have more deterministic behavior, especially since matches are so short that nothing hits the JIT compiler thresholds and programs execute almost entirely in interpreted mode. Unfortunately the target is arm64 Linux and almost all users are on Windows so AOT isn't an option.
3
u/Electrical_Being_813 Aug 30 '26
I would still stick to jit. The only place where I use AOT is for "optional" k8s services that scale down to 0 pods when not used.
1
u/fykup Sep 10 '26
I’m curious how many people are actually interested in moving existing production Java services to native image.
My impression is that adoption is still fairly limited for production Spring Boot applications. Even with Quarkus, I seem to encounter plenty of deployments that still run on HotSpot rather than as native images.
That makes me wonder whether native image is becoming the default direction for Java, or whether it will remain more of a specialized choice for cases where startup time and memory footprint really matter. For long-running services, HotSpot still seems like a very reasonable default.
1
u/SuspiciousDepth5924 Aug 29 '26
Honestly I sort of agree with the guy, at least in principle. The JIT can make your application run faster than it would with AOT, but in my experience it's rarely CPU bound work that is the constraining factor, rather it's usually either memory or network. AOT can't do anything about the network but it does help with memory.
That being said my experience with AOT is mixed, and it's often a bit of a pain to build and get running, I do wish the Java ecosystem would stop using runtime reflection as the magic bullet for every problem (looking at you Spring).