I'm trying to understand what the -XX:G1ReservePercent actually does. The descriptions I found in the official documentation are not really comprehensive:
Sets the percentage of reserve memory to keep free so as to reduce the risk of to-space overflows. The default is 10 percent. When you increase or decrease the percentage, make sure to adjust the total Java heap by the same amount.
and the desciption of to-space exhausted log entry is this:
When you see to-space overflow/exhausted messages in …
在Windows 10 / OpenJDK 11.0.4_x64上运行下面的代码会作为输出used: 197和输出expected usage: 200。这意味着200个字节数组(一百万个元素)占用了大约。200MB RAM。一切都很好。
当我将代码中的字节数组分配从new byte[1000000]更改为new byte[1048576](即更改为1024 * 1024个元素)时,它会作为输出used: 417和产生expected usage: 200。有没有搞错?
import java.io.IOException;
import java.util.ArrayList;
public class Mem {
private static Runtime rt = Runtime.getRuntime();
private static long free() { return rt.maxMemory() - rt.totalMemory() + rt.freeMemory(); }
public static void main(String[] args) throws InterruptedException, IOException {
int blocks = 200;
long initiallyFree = free();
System.out.println("initially free: " + initiallyFree / 1000000);
ArrayList<byte[]> …Run Code Online (Sandbox Code Playgroud)