我的代码是:
public class Memory {
// Dummy Entity representing usual data objects
private static class Entity {
public String name;
public String detail;
public Double amount;
public Integer age;
}
// Linked list offers table inserts and helps illustrating the issue by using multiple
// references per entry
public static java.util.LinkedList<Entity> entities = new java.util.LinkedList<>();
// This threshold should be 2 times Xmn. It ensures the loop stops before a full GC happens.
private static final int MB = 1024 * 1024;
private static final int THRESHOLD = 100 * MB;
public static void main(String[] args) {
System.out.println("Total Memory (in bytes): " + Runtime.getRuntime().totalMemory());
System.out.println("Free Memory (in bytes): " + Runtime.getRuntime().freeMemory());
System.out.println("Max Memory (in bytes): " + Runtime.getRuntime().maxMemory());
while (true) {
appendEntitiesToDataStructure();
terminateBeforeFullGCorOOMEcanHappen();
}
}
private static void appendEntitiesToDataStructure() {
entities.add(new Entity());
}
private static void terminateBeforeFullGCorOOMEcanHappen() {
if (Runtime.getRuntime().freeMemory() < THRESHOLD) {
System.out.println("Elements created and added to LinkedList: " + entities.size());
System.exit(0);
}
}}
启动:
java -Xms${i}g -Xmx${i}g -Xmn50m memory.java >> output.txt
idea :我想知道JVM堆大小在演变时的行为.
堆大小:从1 GB到100 GB
GC测试:G1c、ZGC、PGC
结果:使用g1c和pgc会出现从32 GB开始的回归--随着堆大小的增加,我可以在对象中存储更少的数据.
模式:X:我的对象的大小;Y:堆大小
我不知道该如何解释这种行为-->;你知道吗?