In a container, MaxRAMPercentage is calculated basing on cgroup memory.limit_in_bytes value.

It is limits Kubernetes configuration that affects cgroup memory limit. So, in your case, the maximum heap size will be set to 256M (50% of the limit).

Answer from apangin on Stack Overflow
🌐
Brice Dutheil
blog.arkey.fr › 2020 › 10 › 27 › maxrampercentage-is-not-what-i-wished-for
-XX:MaxRAMPercentage is not what I wished for • Brice Dutheil
October 27, 2020 - This change made the JVM able to read information from the cgroup, and the -XX:*RAMPercentage flag family tells the JVM which proportion of the available memory for this cgroup can be used.
Discussions

What happens when heap size set by XX:MaxRAMpercentage is exceeded?
Hi. I am looking into possibilities of using the parameters XX:MInRAMpercentage and XX:MaxRAMpercentage when determining the heap size in a JVM. Let me show an example: We have the Kubernetes POD named Pod1. Inside Pod1 we have: limits: cpu: “250m” memory: “500Mi” requests: cpu: “250m” ... More on discuss.kubernetes.io
🌐 discuss.kubernetes.io
1
1
August 26, 2021
Should we set -Xmx (max java heap size) in a kubernetes container - Stack Overflow
Is there any rule of thumb of setting ... to kubernets containers? ... FWIW I've deleted all of these in our code and running on relatively modern versions of k8s (as of writing I think it's 1.21 or 1.22) I haven't had to think about these flags.. ... Since Java 10 and Java 8u191 there should be quite good support for Java determining actual memory available in the container. It's not necessary to use -Xmx anymore. It's recommended to use XX:MaxRAMPercentage with a value ... More on stackoverflow.com
🌐 stackoverflow.com
Optimizing Java Memory in Kubernetes: Distinguishing Real Need vs. JVM "Greed" ?
Think less about how much memory the JVM is using and more about how much GC pause time may be impacting your business. Too much GC pause time may indicate your application needs more memory so that GC can work more spread out over time, therefore reducing GC pause time, and therefore reducing impact on your application. When you start linking performance to business goals (SLOs), you will see that memory consumption of the JVM is a consequence of your business needs. The more memory you give to the JVM, the more it will use it to minimize GC pause time (the time taken by the JVM to do GC instead of doing actual work for your application). I talk about Performance Java on Kubernetes in this most recent talk at InfoQ Dev Summit Boston 2024: https://www.infoq.com/presentations/optimizing-java-app-kubernetes/ More on reddit.com
🌐 r/java
56
98
April 17, 2025
java - -XX:MaxRAMPercentage with respect to K8S request and limit - Stack Overflow
If I use -XX:MaxRAMPercentage for Java running in a container, and in the K8S definition I provide request and limit values for the container's memory, what will happen? For example, if the request... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › MihaiBojin › 73186a0e63f7dc5dda58d0abb2388022
JVM heap allocation vs. ResourceQuota configuration in Kubernetes · GitHub
JVM heap allocation vs. ResourceQuota configuration in Kubernetes ... (1) starting the JVM with -XX:MaxRAMPercentage=90 and -XX:+UseContainerSupport results in the correct detection of the container's configured RAM (limits.memory in Kube)
🌐
Kubernetes
discuss.kubernetes.io › general discussions
What happens when heap size set by XX:MaxRAMpercentage is exceeded? - General Discussions - Discuss Kubernetes
August 26, 2021 - Inside Pod1 we have: limits: cpu: “250m” memory: “500Mi” requests: cpu: “250m” memory: “250Mi” If we set XX:MaxRAMpercentage at 50% which means that the heap will be 250 Mi.
🌐
Pretius
pretius.com › home › jvm kubernetes: optimizing kubernetes for java developers
JVM Kubernetes: Optimizing Kubernetes for Java Developers - Pretius
July 14, 2025 - java -XX:MaxRAMPercentage=75 -XX:+UseParallelGC -XX:ActiveProcessorCount=<2x yourCpuLimit> myapp.jar
🌐
Medium
medium.com › @sharprazor.app › memory-settings-for-java-process-running-in-kubernetes-pod-1e608a5d2a64
Memory settings for Java process running in Kubernetes pod | by Fan Liu | Medium
August 30, 2023 - USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND xxx-+ 1 7.7 0.4 24751760 3818536 ? Ssl Jul28 340:41 /usr/java/jdk-11.0.17/bin/java -XX:MaxRAMPercentage=75.0 -XshowSettings:vm -classpath ... xxx-+ 80559 0.0 0.0 50548 3936 ?
🌐
CodingTechRoom
codingtechroom.com › question › understanding-the-xx-maxrampercentage-flag-in-java-10-for-kubernetes-containers
How to Use the JAVA 10+ -XX:MaxRAMPercentage Flag in a Kubernetes Container - CodingTechRoom
The -XX:MaxRAMPercentage flag in Java 10 and later versions allows developers to specify the maximum amount of heap memory the Java Virtual Machine (JVM) can use as a percentage of the total memory available to the container.
Find elsewhere
🌐
Reddit
reddit.com › r/java › optimizing java memory in kubernetes: distinguishing real need vs. jvm "greed" ?
r/java on Reddit: Optimizing Java Memory in Kubernetes: Distinguishing Real Need vs. JVM "Greed" ?
April 17, 2025 -

Hey r/java,

I work in performance optimization within a large enterprise environment. Our stack is primarily Java-based IS running in Kubernetes clusters. We're talking about a significant scale here – monitoring and tuning over 1000 distinct Java applications/services.

A common configuration standard in our company is setting -XX:MaxRAMPercentage=75.0 for our Java pods in Kubernetes. While this aims to give applications ample headroom, we've observed what many of you probably have: the JVM can be quite "greedy." Give it a large heap limit, and it often appears to grow its usage to fill a substantial portion of that, even if the application's actual working set might be smaller.

This leads to a frequent challenge: we see applications consistently consuming large amounts of memory (e.g., requesting/using >10GB heap), often hovering near their limits. The big question is whether this high usage reflects a genuine need by the application logic (large caches, high throughput processing, etc.) or if it's primarily the JVM/GC holding onto memory opportunistically because the limit allows it.

We've definitely had cases where we experimentally reduced the Kubernetes memory request/limit (and thus the effective Max Heap Size) significantly – say, from 10GB down to 5GB – and observed no negative impact on application performance or stability. This suggests potential "greed" rather than need in those instances. Successfully rightsizing memory across our estate would lead to significant cost savings and better resource utilization in our clusters.

I have access to a wealth of metrics :

  • Heap usage broken down by generation (Eden, Survivor spaces, Old Gen)

  • Off-heap memory usage (Direct Buffers, Mapped Buffers)

  • Metaspace usage

  • GC counts and total time spent in GC (for both Young and Old collections)

  • GC pause durations (P95, Max, etc.)

  • Thread counts, CPU usage, etc.

My core question is: Using these detailed JVM metrics, how can I confidently determine if an application's high memory footprint is genuinely required versus just opportunistic usage encouraged by a high MaxRAMPercentage?

Thanks in advance for any insights!

🌐
Ralph's Open Source Blog
ralph.blog.imixs.com › 2020 › 10 › 22 › how-to-optimize-memory-consumption-for-wildfly-running-in-kubernetes
How-to Optimize Memory Consumption for Java Containers Running in Kubernetes - Ralph's Open Source Blog
October 22, 2020 - ..... spec: containers: - name: wildfly image: jboss/wildfly:20.0.1.Final env: - name: JAVA_OPTS value: "-XX:MaxRAMPercentage=75.0" .... If you apply the new java options to your kubernetes Deplyoment you will see the changed output:
🌐
Focused
focused.io › lab › the-no-nonsense-guide-to-jvm-14-memory-on-kubernetes
The No-Nonsense Guide to JVM 14 Memory on Kubernetes | Focused
May 28, 2026 - The JVM will use up to the MaxRAMPercentage of the Node's memory. If the pod approaches the Node's memory limit, Kubernetes will kill the pod rather than throwing an OOM Exception.
🌐
Red Hat
developers.redhat.com › articles › 2022 › 04 › 19 › java-17-whats-new-openjdks-container-awareness
Java 17: What’s new in OpenJDK's container awareness | Red Hat Developer
December 13, 2024 - For example, in a Kubernetes container with a memory limit of 800MB RAM, a default -XX:MaxRAMPercentage=25, probably doesn't make as much sense as it would on a multi-user desktop system, because the maximum heap size would be bound above by ...
🌐
GitHub
gist.github.com › petrbouda › 92d794370134fc9dda1fdc05473c7165
Memory LIMIT and REQUEST in Containers and JVM · GitHub
What if I configure different REQUEST and LIMIT sizes in Kubernetes? We need to somehow calculate Heap Size of our JVM process if it based on ergonomics or percentage ... java -Xlog:os+container=trace -version Picked up JAVA_TOOL_OPTIONS: -XX:InitialRAMPercentage=80 -XX:MaxRAMPercentage=80 -XX:+UseSerialGC [0.001s][trace][os,container] OSContainer::init: Initializing Container Support [0.001s][debug][os,container] Detected cgroups hybrid or legacy hierarchy, using cgroups v1 controllers [0.001s][trace][os,container] Path to /memory.use_hierarchy is /sys/fs/cgroup/memory/memory.use_hierarchy [0
🌐
Xebia
xebia.com › home › blog › a practical guide to kubernetes and jvm integration
A Practical Guide To Kubernetes And JVM Integration | Xebia
3 weeks ago - However, in a Kubernetes territory, it is advised to use the requests and limits parameters to manage the memory allocation of a pod. For example, -XX:MaxRAMPercentage is a soft limit, and the JVM may consume more memory if needed.
🌐
OneUptime
oneuptime.com › home › blog › how to handle container oomkilled by tuning memory limits and jvm heap
How to Handle Container OOMKilled by Tuning Memory Limits and JVM Heap
February 9, 2026 - OOMKilled errors are among the most frustrating issues in Kubernetes, especially for Java applications. When a container exceeds its memory limit, the kernel's Out-Of-Memory killer terminates the process, leading to pod restarts and potential service disruptions.
🌐
Medium
medium.com › @ajeet_ › optimizing-java-memory-management-heap-size-in-kubernetes-avoiding-out-of-memory-errors-e4da3a7c8fab
Optimizing Java Memory Management(Heap-size) in Kubernetes: Avoiding Out of Memory Errors | by Ajeet | Medium
January 9, 2025 - To set the -XX:MaxRAMPercentage=50.0 option, add the JAVA_TOOL_OPTIONS environment variable to your container specification in the Kubernetes pod YAML file.
Top answer
1 of 2
31

The default "max heap" if you don't specify -Xmx is 1/4 (25%) of the host RAM. JDK 10 improved support for containers in that it uses container's RAM limits instead of underlying host. As pointed by @David Maze this has been backported to JDK 8.

Assuming you have a sufficiently recent version of JDK 8, you can use -XX:MaxRAMPercentage to modify the default percentage of total RAM used for Max heap. So instead of specifying -Xmx you can tell, e.g. -XX:MaxRAMPercentage=75.0. See also https://blog.arkey.fr/2020/10/27/maxrampercentage-is-not-what-i-wished-for/

Here's an example using alpine JDK docker image: https://hub.docker.com/_/openjdk (see section "Make JVM respect CPU and RAM limits" in particular).

# this is running on the host with 2 GB RAM
docker run --mount type=bind,source="$(pwd)",target=/pwd -it openjdk:8

# running with MaxRAMPercentage=50 => half of the available RAM is used as "max heap"
root@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -XX:MaxRAMPercentage=50.0 -version | grep -i maxheap
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MaxHeapSize                              := 1044381696                          {product}
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

# running without MaxRAMPercentage => default 25% of RAM is used
root@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -version | grep -i maxheap
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MaxHeapSize                              := 522190848                           {product}
openjdk version "1.8.0_265"
2 of 2
0

In my K8s setup, I am using consul to manage the pod configuration. Here is a command to override the jvm setting on the fly. It is a pretty much project specific but it might give you a hint if you are using consul for configuration.

kubectl -n <namespace> exec -it consul-server -- bash -c "export CONSUL_HTTP_ADDR=https://localhost:8500 && /opt/../home/bin/bootstrap-config --token-file /opt/../config/etc/SecurityCertificateFramework/tokens/consul/default/management.token kv write config/processFlow/jvm/java_option_xmx -Xmx8192m"
🌐
Medium
medium.com › codex › running-jvm-applications-on-kubernetes-beyond-java-jar-a095949f3e34
Running JVM Applications on Kubernetes: Beyond java -jar | by Thiago Mendes | CodeX | Medium
February 7, 2024 - To avoid wasting resources, properly configure the JVM heap size using the -Xmx parameter or the -XX:MaxRAMPercentage flag.