简体   繁体   中英

Is jvm heap memory option `XX:MaxRAMPercentage` only valid for dockerized applications?

As per link https://bugs.openjdk.org/browse/JDK-8186248

The purpose of XX:MaxRAMPercentage is to Allow more flexibility in selecting Heap % of available RAM, ie we need to control the amount of memory consumed by java via limiting container memory, that in turn means following:

  1. for the vast majority of java applications the value of -XX:MaxRAMPercentage should be about 80-95 because we would like to consume all container memory.

Does that mean -XX:MaxRAMPercentage shouldn't be used if application is NOT running in a container? I thought if there is no container involved then also these options could be used.

Can anyone clarify?

There is no technical obstacle preventing you from using this option without containers. When I use the following program

import com.sun.management.OperatingSystemMXBean;
import static java.lang.management.ManagementFactory.*;

public class PrintMaxRAM {
    public static void main(String[] args) {
        long maxHeap = getMemoryMXBean().getHeapMemoryUsage().getMax();
        long maxRAM = getPlatformMXBean(OperatingSystemMXBean.class)
            .getTotalMemorySize();
        System.out.println(maxHeap + " (" + maxHeap * 100 / maxRAM + "%)");
    }
}

like

jdk-17\bin\java PrintMaxRAM.java

it prints

3737124864 (25%)

on my machine (Windows, no virtualization). When I use

jdk-17\bin\java -XX:MaxRAMPercentage=75 PrintMaxRAM.java

instead, it prints as expected:

11207180288 (75%)

(there's obviously some rounding to block sizes going on, so the number of bytes is not exactly a triple)

So whether you “should” use this option, is about your own (our your customer's) intention .

  • When you run the application on a non-virtualized system, together with unknown programs, you probably want to limit your absolute maximum memory consumption, eg via -Xmx , and leave the unknown remaining amount of memory for the unknown programs.

  • When you run the application in a dedicated container, together with a known set of programs or no other programs at all, you most probably want to specify the maximum amount of memory in relation to the container's memory, so when you want to change the available memory, you only have to reconfigure the container instead of needing to adapt all programs' start configurations.

So it's not a question of right or wrong but about the actual use case. For example, to specify an absolute upper bound for the required memory, you have to know the actual application. That's why the default configuration is a relative value (in some environments) even when not running inside a container, as the JVM can't predict a useful absolute value for an arbitrary application.

If, for example, you are not using containers, but the entire hardware is dedicated to run a single program, it makes perfect sense to use, eg -XX:MaxRAMPercentage=90 , for that program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM