简体   繁体   中英

Java set maximum heap size (Xmx) as a fraction of physical memory

In JDK 8 , The default maximum heap size is

1/4th of the physical memory or 1GB

And it can be overridden using the -Xmx switch:

You can override this default using the -Xmx command-line option.

The Xmx switch works with bytes , not fractions. Is there a straightforward way of setting the maximum heap to X percent of the physical memory of a machine?

Edit:

Why, you ask? Well, first of all, curiosity. I find it strange that the default Xmx is defined in terms of a fraction of the physical memory, but as soon as I touch it, then it's an absolute value. Second, if I deploy the same (or similar) app to two different servers, and one of them has more physical memory, I'd like to have the option of automatically getting the JVM to take advantage of the extra memory. It doesn't always make sense, but sometimes it does.

The obvious solution is to use a shell script that checks the amount of free space and calculates the Xmx, but that seems like a clunky solution. I imagine it should be somehow possible with pure Java.

Since Java 8u191 (October 16, 2018) , there are three dedicated JVM options to control the heap size as a fraction of the available memory. They are documented here .

Note that contrary to what its name suggests, MinRAMPercentage sets the maximum heap size

-XX:InitialRAMPercentage=percent

Sets the initial amount of memory that the JVM will use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option. The default value is 1.5625 percent.

-XX:MaxRAMPercentage=percent

Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option. The default value is 25 percent.

-XX:MinRAMPercentage=percent

Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option for small heaps. A small heap is a heap of approximately 125 MB. The default value is 50 percent.

Here are some example runs using docker on x64 using jdk 11u10:

docker run --memory <limit> openjdk:11 java -XX:MaxRAMPercentage=25 -XX:MinRAMPercentage=50 -XX:InitialRAMPercentage=5 -XX:+PrintFlagsFinal 2>&1 | grep HeapSize

| Memory Limit | Initial Heap Size | Max Heap Size    |
|--------------|-------------------|------------------|
| 100Mi        | 10485760 (~10%)   | 52428800 (~50%)  |
| 256Mi        | 27262976 (~10%)   | 132120576 (~50%) |
| 512Mi        | 54525952 (~10%)   | 134217728 (~25%) |
| 1Gi          | 109051904 (~10%)  | 268435456 (~25%) |

You probably have to do it yourself. Say you're going for a windows version, get the max amount of Memory ( parse something like wmic ComputerSystem get TotalPhysicalMemory ) , calculate 1/4 of this and set it as the -Xmx option...

I would like to ask you why you'd need a percentage of the physical memory. The max amount of memory shouldnt affect the memory usage of your application, and if so the application should handle it itself

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