r/java • u/danielepolencic • 3d ago
Java JVM CPU and Memory Requests and Limits in Kubernetes
Running Java on Kubernetes? CPU and memory limits affect much more than scheduling.
A 4 GiB heap requires a container larger than 4 GiB. Fractional CPU limits can also change the processor count that HotSpot sees.
In our new article, you will learn:
- Why JVM heap size and container memory are different boundaries
- How CPU limits affect garbage collection, worker pools, and application performance
- Which JVM and container metrics to collect when validating resource settings under load
The article also includes practical experiments and an interactive configuration calculator.
Read: https://learnkube.com/java-jvm-kubernetes-requests-limits
2
u/TallGreenhouseGuy 3d ago
Buildpacks take this into account in the memory formula, which could be a good starting point:
https://paketo.io/docs/reference/java-reference/#memory-calculator
2
1
u/burl-21 2d ago
And it should soon add low memory profile too, see https://github.com/paketo-buildpacks/jvm-vendors/pull/31
2
u/Turbots 2d ago
Use buildpacks to build your Java containers, it has a really good memory calculator in there to compute the JVM heap and offheap sizes really well, and you can tweak them very well too.
Or just the memory calculator in your own docker file:
https://github.com/cloudfoundry/java-buildpack-memory-calculator
1
u/k_brn 3d ago
But do you really need Kubernetes if you can run on two 1 GB VMs with a reverse proxy and get better availability with way less management overhead?
3
u/thisisjustascreename 3d ago
The answer to "Do you really need Kubernetes?" is almost always no. But lots of people know how to run it and lots of orgs are already using it.
7
u/Cilph 3d ago
If you just have the one app, you likely dont need it. If you need to deploy multiple apps over multiple solated environments for multiple tenants and set up secure firewalls between each and support blue-green deployment at the same time, then it quickly becomes reasonable to use k8s.
24
u/cogman10 3d ago
I skimmed the article, and didn't really notice a mention of JVM off heap allocations. Those are a pretty big deal that can make right sizing the JVM tricky.
This, IMO, is a weakness in JVM auto-configuration. The JVM just does a poor job in selecting reasonable memory limits in a container environment. It's forced us to explicitly set the values because everything is just a little wrong.
MaxRamPercentage seems like it's a good thing, but it fails to account for the fact that not everything grows at the same rate. 10% headroom might not be enough when talking about a container with a 256MB max, but it may be extreme overkill if you are talking about a container with a 64GB max.
It'd be really nice if the JVM could automatically and dynamically adjust it's XMX setting to max out memory usage. What I'd like would be able to say something like "JVM, Start with an XMX that's 75% the container's memory, watch and see how your native memory use grows. Slowly scale up that XMX until the system's free memory is around 100MB"