我的代码:
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
想法:我想了解JVM堆大小在演变时的行为。
堆大小:从1到100 GB
测试的GC:g1c,zgc,pgc
结果:使用g1c和pgc时,从32GB开始存在回归->随着堆大小的增加,我可以存储更少的数据在我的对象中。
模式:X:我的对象的大小;Y:堆大小
我不知道如何解释这种行为->有什么想法吗?