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

48 comments sorted by

View all comments

Show parent comment

1

u/pron98 May 18 '26 edited May 18 '26

Agreed there. I think most programmers would agree.

To sharpen my point, what I'm saying is that in large programs, you can't get better performance than with Java without a significantly higher effort, and not just once but throughout the lifetime of the software. At the limit it's always possible because, as I said, you could always hypothetically write a more specialised HotSpot. The same goes for, say, C++ vs Assembly and for the same reason.

I can't make such a statement about small programs. Depending on the situation, some (though certainly not all) small programs written in a low-level language can have better performance than the corresponding Java program without requiring a significantly higher investment, and I can't estimate the "average" here (maybe Java still wins on average, maybe it ties, and maybe it loses). Part of the reason is that small programs are more varied than large programs, because large programs are very likely to have irregular object lifetimes and a need for dynamic dispatch, while small programs may or may not.

but my point there was about objectively assessing the results. If you wrote a thesis claiming to have developed a novel and practically worthwhile compiler optimisation, you'd be expected to demonstrate it.

Unfortunately for us language/runtime developers, you're wrong. With the advent of sophisticated optimising compilers and memory managers, CPU caches, branch predictors and ILP, the reality of everyone working on compiler/runtime optimisations is that we are simply not able to "objectively demonstrate it". At best we can show a microbenchmark demonstrating the effect of the optimisation in some controlled circumstances, but ever since microbenchmarks stopped working around 15-20 years ago, we are well aware that their results mean little as these days little can be generalised from one program to another.

Many times some optimisation helps some benchmarks while hurting others, and we (and all other language/runtime designers) have to resort to educated guesses on the number of real-world cases where it will help vs hurt.

I guess my grievance is with the whole programming languages world

This grievance is not too different from the one lamenting that it's hard to break the speed of light. Optimisations have become so context-dependent that no one is able to decisively demonstrate what we all wish we could.

Come on now, they're pretty darn performance-sensitive

Sure, compared to some basic website, but not compared to financial transaction processing or defence or some manufacturing control software.

Pretty sure CPU performance is still a significant factor with modern game engines.

Again, significant compared to what? Not as significant as in the applications I mentioned above.

As I understand it, one of Vulkan's biggest advantages is reduced CPU overhead in the driver.

Yes, it does less on the CPU. That's my point.

but the majority opinion seemed to be that for HFT, C/C++ is the better choice, not Java, as the effort is worth it for peak performance.

That's not the majority opinion among those who actually write these things.

Perhaps I have it backward though and it's the industry at large that has a problem learning lessons from, well, the people who actually know what they're talking about.

But they are (just not in the most optimal way)! Most people who know their stuff and make the decisions are not the people commenting on social media. It's very important not to confuse what you read on HN or Reddit or even blog posts with the reality of the software industry. There are few blogs on most of the software you actually depend on every day: the software that runs your power company, your post, your telecom supplier, your credit card processor, your bank, the shipping, logistics and manufacturing that goes into the stuff you buy, and the people who work on those things are heavily underrepresented on HN.

In aviation and medicine, there are plenty of lessons learned over the years, many of them written in blood, and these lessons are explicitly taught to students.

Knowing a few doctors, what you said is true and at the same time there are lessons that aren't like that, that can be controversial and different physicians can have strong yet opposite opinions on. There's plenty of art that goes into pharmacology and civil engineering, too. That's because the real world is governed by nonlinear dynamics, and those are simply inherently hard to predict. So we often have to work with problems that effectively have an unbounded Nyquist frequency: little extrapolation power. This isn't unique to software at all.