r/java • • 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.

22 Upvotes

43 comments sorted by

View all comments

Show parent comment

9

u/za3faran_tea Aug 28 '26

I think he touched at this point. He was saying that a lot of these benefits can be reached using PGO, and re-optimization is very rare in practice.

7

u/Thirty_Seventh Aug 29 '26

unfortunately, PGO isn't available in Community Edition and therefore does not exist as far as I'm concerned

4

u/koflerdavid Aug 28 '26

I'm very doubtful that PGO can effectively capture the effects of, say, changing runtime configuration parameters. Those are effectively constant for most of the runtime of the application until they, well, change. Perfect for the JIT who can now specialize on them. Meanwhile for PGO if you have more than, say, three booleans then the effort of collecting profiling data for all combinations becomes prohibitive.

6

u/v4ss42 Aug 28 '26

And I'm saying I disagree with his extraordinary assertion that AOT optimization is as good as JIT.

4

u/koflerdavid Aug 28 '26

It can be good. After all AOT is what languages like C++ and Rust have been doing all along. Of course Java has a few features that make it harder to optimize.

5

u/v4ss42 Aug 28 '26

You'll notice that I was careful to make a comparative statement that AOT optimization can't be as good as JIT optimization. What I didn't say is that AOT optimization can't be "good" in some absolute sense - yes it can be quite adequate, and not only on the JVM, but the reality is that AOT optimization can't touch JIT optimization because it has zero visibility into the runtime behavior of the code.

2

u/koflerdavid Aug 29 '26

That's where Profile Guided Optimization comes in, which works as long as the real workload doesn't drift too much from the profile workload. That's its biggest disadvantage of course - a JIT compiler can adapt to changing circumstances.

5

u/v4ss42 Aug 29 '26

Right but PGO both lags the runtime context and has poor developer ergonomics. For the majority of JVM hosted apps (i.e. long running server processes running on beefy hardware) JIT remains a better general approach.

2

u/Electrical_Being_813 Aug 30 '26

If you are forced to use PGO, you can as well use JIT. It will be less messy and will do a better job.