问一个JVM的问题,测试结果跟自己的理解有误
public class Test { public static final int MB = 1024 * 1024; public static void main(String[] args) throws InterruptedException { byte[] a1 = new byte[2 * MB]; byte[] a2 = new byte[2 * MB]; byte[] a3 = new byte[2 * MB]; byte[] a4 = new byte[2 * MB]; } }参数设置: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8结果如下:[GC (Allocation Failure) [PSYoungGen: 7127K->648K(9216K)] 7127K->6800K(19456K), 0.0042678 secs] [Times: user=0.03 sys=0.02, real=0.00 secs] [Full GC (Ergonomics) [PSYoungGen: 648K->0K(9216K)] [ParOldGen: 6152K->6670K(10240K)] 6800K->6670K(19456K), [Metaspace: 2594K->2594K(1056768K)], 0.0074118 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] Heap PSYoungGen total 9216K, used 2130K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) eden space 8192K, 26% used [0x00000000ff600000,0x00000000ff814930,0x00000000ffe00000) from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000) to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000) ParOldGen total 10240K, used 6670K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) object space 10240K, 65% used [0x00000000fec00000,0x00000000ff283bf8,0x00000000ff600000) Metaspace used 2600K, capacity 4486K, committed 4864K, reserved 1056768K class space used 288K, capacity 386K, committed 512K, reserved 1048576K
新生代采用的是 Parallel Scavenge,书上说是 标记复制算法
那么a1,a2,a3分配到了 Eden区,当分配a4的时候发觉Eden不够空间了,那么进行新生代GC,
那么新生代的对象就晋升到了老年代嘛,那么新生代完全足够容得下a4。 为何还要进行一次Full GC。
周志明老师的那本《深入理解Java虚拟机》第93页的那个实验,也没有发生Full GC。所以为啥呢?
ps. jdk版本为1.8