1. Overview
1.概述
In this tutorial, we’ll discuss a few JVM parameters we can use to set the RAM percentage of the JVM.
在本教程中,我们将讨论几个JVM参数,我们可以用来设置JVM的RAM百分比。
Introduced in Java 8, the parameters InitialRAMPercentage, MinRAMPercentage, and MaxRAMPercentage help to configure the heap size of a Java application.
在Java 8中引入的参数InitialRAMPercentage、MinRAMPercentage和MaxRAMPercentage有助于配置Java应用程序的堆大小。
2. -XX:InitialRAMPercentage
2.-XX:InitialRAMPercentage
The InitialRAMPercentage JVM parameter allows us to configure the initial heap size of the Java application. It’s a percentage of the total memory of a physical server or container, passed as a double value.
InitialRAMPercentageJVM参数允许我们配置Java应用程序的初始堆大小。这是一个物理服务器或容器的总内存的百分比,以双倍的数值传递。
For instance, if we set-XX:InitialRAMPercentage=50.0 for a physical server of 1 GB full memory, then the initial heap size will be around 500 MB (50% of 1 GB).
例如,如果我们设置-XX:InitialRAMPercentage=50.0为1GB全内存的物理服务器,那么初始堆大小将是500MB左右(1GB的50%)。
To start with, let’s check the default value of the IntialRAMPercentage in the JVM:
首先,让我们检查一下JVM中IntialRAMPercentage的默认值。
$ docker run openjdk:8 java -XX:+PrintFlagsFinal -version | grep -E "InitialRAMPercentage"
double InitialRAMPercentage = 1.562500 {product}
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
Then, let’s set the initial heap size of 50% for a JVM:
然后,让我们为一个JVM设置50%的初始堆大小。
$ docker run -m 1GB openjdk:8 java -XX:InitialRAMPercentage=50.0 -XX:+PrintFlagsFinal -version | grep -E "InitialRAMPercentage"
double InitialRAMPercentage := 50.000000 {product}
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
It’s important to note that the JVM ignores InitialRAMPercentage when we configure the -Xms option.
值得注意的是,当我们配置-Xms选项时,JVM会忽略InitialRAMPercentage。
3. -XX:MinRAMPercentage
3.-XX:MinRAMP百分比
The MinRAMPercentage parameter, unlike its name, allows setting the maximum heap size for a JVM running with a small amount of memory (less than 200MB).
MinRAMPercentage参数与它的名字不同,它允许为运行着少量内存的JVM设置最大堆大小(小于200MB)。
First, we’ll explore the default value of the MinRAMPercentage:
首先,我们将探讨MinRAMP百分比的默认值。
$ docker run openjdk:8 java -XX:+PrintFlagsFinal -version | grep -E "MinRAMPercentage"
double MinRAMPercentage = 50.000000 {product}
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
Then, let’s use the parameter to set the maximum heap size for a JVM with a total memory of 100MB:
然后,让我们使用该参数为一个总内存为100MB的JVM设置最大堆大小。
$ docker run -m 100MB openjdk:8 java -XX:MinRAMPercentage=80.0 -XshowSettings:VM -version
VM settings:
Max. Heap Size (Estimated): 77.38M
Ergonomics Machine Class: server
Using VM: OpenJDK 64-Bit Server VM
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
Also, the JVM ignores the MaxRAMPercentage parameter while setting the maximum heap size for a small memory server/container:
另外,JVM在为小内存服务器/容器设置最大堆大小时,忽略了MaxRAMP百分数参数。
$ docker run -m 100MB openjdk:8 java -XX:MinRAMPercentage=80.0 -XX:MaxRAMPercentage=50.0 -XshowSettings:vm -version
VM settings:
Max. Heap Size (Estimated): 77.38M
Ergonomics Machine Class: server
Using VM: OpenJDK 64-Bit Server VM
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
4. -XX:MaxRAMPercentage
4. -XX:MaxRAMPercentage
The MaxRAMPercentage parameter allows setting the maximum heap size for a JVM running with a large amount of memory (greater than 200 MB).
MaxRAMPercentage参数允许为使用大量内存运行的JVM设置最大堆大小(大于200MB)。
First, let’s explore the default value of the MaxRAMPercentage:
首先,让我们探讨一下MaxRAMP百分数的默认值。
$ docker run openjdk:8 java -XX:+PrintFlagsFinal -version | grep -E "MaxRAMPercentage"
double MaxRAMPercentage = 25.000000 {product}
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
Then, we can use the parameter to set the maximum heap size to 60% for a JVM with 500 MB total memory:
然后,我们可以使用该参数将一个总内存为500MB的JVM的最大堆大小设置为60%。
$ docker run -m 500MB openjdk:8 java -XX:MaxRAMPercentage=60.0 -XshowSettings:vm -version
VM settings:
Max. Heap Size (Estimated): 290.00M
Ergonomics Machine Class: server
Using VM: OpenJDK 64-Bit Server VM
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
Similarly, the JVM ignores the MinRAMPercentage parameter for a large memory server/container:
类似地,JVM忽略了大内存服务器/容器的MinRAMPercentage参数。
$ docker run -m 500MB openjdk:8 java -XX:MaxRAMPercentage=60.0 -XX:MinRAMPercentage=30.0 -XshowSettings:vm -version
VM settings:
Max. Heap Size (Estimated): 290.00M
Ergonomics Machine Class: server
Using VM: OpenJDK 64-Bit Server VM
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
5. Conclusion
5.总结
In this short article, we discussed the use of JVM parameters InitialRAMPercentage, MinRAMPercentage, and MaxRAMPercentage for setting the RAM percentages that the JVM will use for the heap.
在这篇短文中,我们讨论了JVM参数InitialRAMPercentage的使用。MinRAMPercentage,以及MaxRAMPercentage用于设置JVM将用于堆的RAM百分比。
First, we checked the default values of the flags set on the JVM. Then, we used the JVM parameters to set the initial and maximum heap sizes.
首先,我们检查了JVM上设置的标志的默认值。然后,我们使用JVM的参数来设置初始和最大的堆大小。