65

Reference: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8186315

I'm really struggling to find out what MinRAMPercentage does, especially compared to InitialRAMPercentage.

I assumed that InitialRAMPercentage sets the amount of heap at startup, that MinRAMPercentage and MaxRAMPercentage set the bottom and top limit of heap that the JVM is allowed to shrink/grow to.

Apparently that is not the case. When I start a JVM (with UseContainerSupport, having these new memory setting parameters) like so:

java -XX:+UseContainerSupport -XX:InitialRAMPercentage=40.0 -XX:MinRAMPercentage=20.0 -XX:MaxRAMPercentage=80.0 -XX:+PrintFlagsFinal -version | grep Heap

InitialHeap and MaxHeap get set, there is no "Minimum Heap Size" value that I can find; Consequently, that MinRAMPercentage never seems to get used.

Super confused, and apparently, I'm not the only one; the OpenJ9 dudes seem to also not fully parse the intent of these options, as I've gathered here and here. They seem to have opted to simply not implement MinRAMPercentage afaics.

So: What is the real intended usage and effect of setting MinRAMPercentage?

Rubin Simons
  • 1,556
  • 1
  • 13
  • 20

2 Answers2

99

-XX:InitialRAMPercentage is used to calculate initial heap size when InitialHeapSize / -Xms is not set.

It sounds counterintuitive, but both -XX:MaxRAMPercentage and -XX:MinRAMPercentage are used to calculate maximum heap size when MaxHeapSize / -Xmx is not set:

  • For systems with small physical memory MaxHeapSize is estimated as

    phys_mem * MinRAMPercentage / 100  (if this value is less than 96M)
    
  • Otherwise (non-small physical memory) MaxHeapSize is estimated as

    MAX(phys_mem * MaxRAMPercentage / 100, 96M)
    

The exact formula is a bit more complicated as it also takes other factors into account.

Note: the algorithm for calculating initial and maximum heap size depends on the particular JVM version. The preferred way to control the heap size is to set Xmx and Xms explicitly.

See also this question.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • How you explain this? Command: ```sudo docker run --memory='10m' adoptopenjdk/openjdk11 java -XX:+PrintFlagsFinal -version | grep MaxHeapSize``` Result: ```Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport size_t MaxHeapSize = 8388608 {product} {ergonomic} openjdk version "11" 2018-09-25 OpenJDK Runtime Environment AdoptOpenJDK (build 11+28) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11+28, mixed mode)``` According your explanation MaxHeapSize should be 5M not 8M – Michał Mielec Mar 21 '19 at 12:22
  • 5
    @MichałMielec As I've told, there are other factors, such as [alignment](https://stackoverflow.com/a/24228242/3448419) and [default generation sizes](http://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/hotspot/share/gc/shared/gc_globals.hpp#l703). They come on stage when the memory limit is so small. – apangin Mar 21 '19 at 13:46
  • Isn't InitialHeapSize/MaxHeapSize always set and used as Xms/Xms if they are not provided explicitly? – hi_my_name_is Apr 09 '19 at 10:49
  • @freakman Not sure I understand what you mean. Please ask a separate question, describe your problem and provide an example. – apangin Apr 09 '19 at 12:48
  • One thing I don't understand is why are there separate MinRAMPercentage and MaxRAMPercentage? What's the point of separating memory to 2 range of below and above 100 MB? – tkalvin Jun 12 '23 at 08:49
1

Depends on your container memory also, So lets suppose you have container memory as 1 GB then in this case -XX:MaxRAMPercentage=80 will be used to determine the max heap ~ 800mb heap memory will be used

And suppose you have container memory less than 250mb then -XX:MinRAMPercentage=20.0 will be used ~ 50mb heap memory will be used

use this article to understand more XX:MinRAMPercentage,XX:MaxRAMPercentage

anil kumar
  • 21
  • 2