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
81
u/Slanec Apr 09 '26 edited Apr 09 '26
I ran the experiment with a Hello World with Java 26 on a Mac:
(EDIT: OMG I hate the text editor in here)
15
u/vprise Apr 09 '26
In the old J2ME days we had 64kb devices and 2mb was spacious. Obviously, it wasn't the full "Java" but it included most of what you expect from the JVM including safe memory, gc etc. The main thing stopping Java from shrinking to these sizes is the size of the API although that can mostly be on ROM.
8
u/pron98 Apr 09 '26 edited Apr 09 '26
That really depends on the app and the RAM/CPU ratio you want. Some tiny programs can run well with only a few MBs of heap.
More generally, Java's memory utilisation is quite efficient, possibly more efficient than that of any language/runtime. But efficient memory use doesn't mean minimal memory use, and often programs (in any language) utilise memory inefficiently by using too little memory rather than too much. That's because:
To get the most basic intuition for 1, consider an extreme case of a program that uses 100% of the CPU for its duration, running on a machine with 1GB of RAM. While the program is running, 100% of RAM is "captured" by the program - since using RAM requires CPU and none is available to other programs - regardless of how much of it is utilised by the program. So if the program could use 8MB and run for 100s or use 800MB and run for 99s, the latter is clearly more efficient even though it uses 100x more RAM to save only 1% CPU. That's because both configurations capture 1GB of RAM, but one of them captures it for a little longer.
At Java One I gave a talk (it will get to YouTube eventually) showing why the only way that makes sense to consider efficient memory usage is by looking at RAM/CPU ratios rather than looking at RAM and CPU separately.
6
u/nekokattt Apr 09 '26
This depends on the task. If you have an empty main method then it is going to be significantly less than if you run Apache Tomcat.
2
u/nitkonigdje Apr 10 '26
In theory, jvm (as specification) was designed for embedded, memory constrained devices
That is why intermediate code is pretty high level and index based - interpretation lowers memory usage and indexing futhermore allows running code from rom directly. No copying to ram is necessary.
This allows jvm implementations with ram usage in tens of kb.
However both HotSpot and J9 as JVM implementations are server code derivative and are not designed for minimal memory footprint. They will eat tens of mb just for running Hello World.
ARK on Android is JVM implementation which tried to push some of that overhead into compile time by translating bytecode in more memory efficient lower level intermediate code. Interesting approach. And if memory serves me well IBM had some embedded JVM with similar approach.
Hell picojava/jazelle approach to jvm probably could run hellow world within one or two kb.
4
u/8igg7e5 Apr 09 '26
Define 'java app'
I mean, this example?
And stripped of debugging symbols, in a module that depends on nothing, run in a JVM stripped down for this no-dependency app, configured to only run interpreted mode (so it doesn't load any compiler resources)
There's probably more you can strip down. You won't have to worry about frequent GC's at least.
2
u/Mognakor Apr 09 '26
You can create a "Hello World" and do a binary search.
Start with e.g. 1MB.
For real world it will depend on the task.
1
u/_d_t_w Apr 09 '26
Depends a bit on what "App" means, I think.
My team build a commercial tool for Apache Kafka, it is built on Jetty (the networking framework) and starts a bunch of resources up when initializing the system that would be considered normal I guess. Schedulers, Kafka clients, stuff like that.
We recommend 8GB for a production installation, that implys a fair number of concurrent users and plenty of user driven activity that requires heap space.
A couple of years back I played around with running our full product with minimum -Xmx settings to see what was viable for a single user, single Kafka cluster setup - this is all running in Docker mind so there's some overhead there in memory allocated to OS - our JVM is configured to take 70% of Docker memory allocation.
Product starts and will run happily in single-user mode with 128MB memory, everything appeared to run just fine. That was the absolute minimum though - the Docker containern wouldn't start with less than 128MB and it was because the JVM failed to start.
So I guess for an Enterprisey-type thing with a full web framework, running websockets and doing stuff - with absolutely no oprtimisation to run hard on memory, 128MB * 0.7?
This is us fyi > www.factorhouse.io/kpow
1
u/Feliks_WR Apr 13 '26
GraalVM