[英]Java set maximum heap size (Xmx) as a fraction of physical memory
In JDK 8 , The default maximum heap size is在JDK 8 中,默认的最大堆大小为
1/4th of the physical memory or 1GB物理内存的 1/4 或 1GB
And it can be overridden using the -Xmx
switch:并且可以使用-Xmx
开关覆盖它:
You can override this default using the -Xmx command-line option.您可以使用 -Xmx 命令行选项覆盖此默认值。
The Xmx
switch works with bytes , not fractions. Xmx
开关适用于bytes ,而不是分数。 Is there a straightforward way of setting the maximum heap to X percent of the physical memory of a machine?有没有一种直接的方法可以将最大堆设置为机器物理内存的 X%?
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.我觉得奇怪的是默认 Xmx 是根据物理内存的一小部分定义的,但是一旦我接触它,它就是一个绝对值。 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.其次,如果我将相同(或类似)的应用程序部署到两台不同的服务器,并且其中一台具有更多的物理内存,我希望可以选择自动让 JVM 来利用额外的内存。 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.显而易见的解决方案是使用 shell 脚本来检查可用空间量并计算 Xmx,但这似乎是一个笨拙的解决方案。 I imagine it should be somehow possible with pure Java.我想它应该可以用纯 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.自Java 8u191(2018 年 10 月 16 日)以来,有三个专用的 JVM 选项来控制堆大小作为可用内存的一部分。 They are documented here .它们记录在此处。
Note that contrary to what its name suggests, MinRAMPercentage
sets the maximum heap size请注意,与其名称所暗示的相反, MinRAMPercentage
设置了最大堆大小
-XX:InitialRAMPercentage=percent -XX:InitialRAMPercentage=百分比
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.在应用人体工程学启发式算法之前,将 JVM 将用于 Java 堆的初始内存量设置为 -XX:MaxRAM 选项中所述确定的最大内存量的百分比。 The default value is 1.5625 percent.默认值为 1.5625%。
-XX:MaxRAMPercentage=percent -XX:MaxRAMPercentage=百分比
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.在应用人体工程学启发式算法之前,将 JVM 可用于 Java 堆的最大内存量设置为 -XX:MaxRAM 选项中所述确定的最大内存量的百分比。 The default value is 25 percent.默认值为 25%。
-XX:MinRAMPercentage=percent -XX:MinRAMPercentage=百分比
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.在应用人体工程学启发式算法之前,将 JVM 可用于 Java 堆的最大内存量设置为在小堆的 -XX:MaxRAM 选项中所述确定的最大内存量的百分比。 A small heap is a heap of approximately 125 MB.小堆是大约 125 MB 的堆。 The default value is 50 percent.默认值为 50%。
Here are some example runs using docker on x64 using jdk 11u10:以下是使用 jdk 11u10 在 x64 上使用 docker 运行的一些示例:
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...假设您要使用 Windows 版本,获取最大内存量(解析类似wmic ComputerSystem get TotalPhysicalMemory
),计算其中的 1/4 并将其设置为 -Xmx 选项...
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最大内存量不应影响应用程序的内存使用量,如果是这样,应用程序应自行处理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.