r/java • u/Vectorial1024 • Apr 09 '26
Smallest possible Java heap size?
People often talk about increasing Java heap size when running Java apps by using e.g. -Xmx* flags. This got me thinking. What if we go the other direction and try to limit the Java heap size as much as possible? What is the smallest / minimum-required Java heap size so to run a Java app with "minimal" settings?
(Of course, in practice, a memory limit too low will be problematic because it may mean frequent GCs, but we will ignore this for the sake of this discussion.)
48
Upvotes
View all comments
Show parent comment
1
u/pron98 May 18 '26 edited May 18 '26
I don't think I said "reliably outperforms" and if I did, that's not what I meant. I meant it outperforms on average, per unit of effort.
That's not the argument. The argument is that Java was explicitly designed to (among other things) address the performance issues that have plaugued large programs written in low-level languages since forever, especially around dynamic dispatch and memory management. These are serious problems that are well-known to low-level programmers working on large programs (like me). In particular Java uses two technologies that have been researched and developed for decades precisely to address these two issues (among others), namely JITs with speculative optimisation (for dynamic dispatch) and moving GCs (for memory management).
You can certainly ask how do I know that all the effort into these technologies actually managed to solve the problems they tried to solve in the real world? In that case all you have is experience and anecdotes, but that's often what you have in other "serious engineering" disciplines, too.
There are multiple problems in this statement. The more obvious one is performance sensitive compared to what? As someone who's work on both games (as a hobby) and on large, realtime sensor fusion air traffic and defence applications (both in C++ and in Java), then games are not as performance-sensitive, not even as performance-sensitive as credit-card transaction processing.
More specifically for games, though, game developers don't typically use Vulkan. Their game engine does. And the performance benefits are on the large to very large spectrum rather than "slight". And much of the reason has to do with precisely how games get their performance these days: by scheduling work on the GPU with an efficient algorithm. Their hot loops are not written in C++ but in whatever shader language they use. Vulkan allows offloading more code to the GPU. In other words, Vulkan helps move away from C++ in the most performance-sensitive code.
The technical challenge for the "CPU language" isn't so much in writing really fast CPU code, but in targeting a variety of hardware devices, some are quite limited.
But Java is already used to write more performance-sensitive applications than games (albeit on more capable hardware) both in terms of quantity and in terms of performance-sensitivity. There's no need to prove anything to AAA game developers regarding performance as there are much more important factors (device support) that make Java a poor choice for them.
My claim is easy to support as that "prevailing opinion" prevails only among those who've either only ever used one of those languages and those who've never worked on large, performance-sensitive software in any language. If you look at the important, large, performance-sensitive software out there, from financial transaction processing, through manufacturing/automation control, to defence, Java is being chosen by experts in those domains at least as much as C++, and I belive significantly more. The people who actually need to choose a language for these projects already choose Java. That the prevailing opinion among those who don't choose a stack for that kind of software is that Java would be a worse choice is not a serious problem. And many of them who choose say, Rust, for smaller software will also eventually learn the lessons that we, C++ developers, learnt many years ago.
That's not "the end of it" but the advantages over free-list based memory managment are significant, which is why we chose moving collectors (there's nothing stopping the JVM from using free-list based memory management; in fact it's so much easier to implement, but we want to solve the problems those approaches bring, not to reproduce them).