1.在Arguments 设置 VM Arguments:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
2.测试代码及日志打印
public class MinorGCExample {
private static final int _1MB=1024*1024;
public static void testAllocation() {
byte[] allocation1,allocation2,allocation3,allocation4;
allocation1=new byte[2*_1MB];
allocation2=new byte[2*_1MB];
allocation3=new byte[2*_1MB];
allocation4=new byte[4*_1MB];
}
public static void main(String[] args) {
MinorGCExample.testAllocation();
}
}
日志打印:
Heap
PSYoungGen total 9216K, used 7128K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 87% used [0x00000000ff600000,0x00000000ffcf6038,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
Metaspace used 2647K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 285K, capacity 386K, committed 512K, reserved 1048576K
3.日志分析:对象优先在Eden分配
I.当新生对象在Eden区中分配。当Eden区没有足够空间进行分配时,虚拟机会进行一次Minor GC(新生代)
||.Arguments VM参数介绍:-XX:PrintGCDetails收集器日志参数,告诉虚拟机进行垃圾回收打印回收日志,程序退出时输出当 前内存区域分配情况。
|||.testAllocation()方法中,尝试分配3个2MB大小和一个4MB大小,-Xms20M、-Xmx20M、-Xmn10M这三个参数限制了堆的 大小不可扩展,新生代分配10M,剩余的10M分配给老年代。-XX:SurvivorRatio=8e决定了新生代中的Eden与Survivor区的空 间比例为8:1