实验:老年代空闲空间放不下minorgc晋升的对象发生fullgc

实验代码

 /**
     * -Xmn10M -Xms20M -Xmx20M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15
     * -XX:PretenureSizeThreshold=3M -XX:+UseParNewGC -XX:+UseConcMarkSweepGC
     * -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
     * @param args
     */
    public static void main(String[] args) {
        byte[] array1 = new byte[4 * 1024 * 1024]; // 4M > 3M 创建大对象,直接进入老年代
        array1 = null;

        byte[] array2 = new byte[2 * 1024 * 1024];
        byte[] array3 = new byte[2 * 1024 * 1024];
        byte[] array4 = new byte[2 * 1024 * 1024];
        byte[] array5 = new byte[128 * 1024];

        byte[] array6 = new byte[2 * 1024 * 1024];
    }

gc日志分析

0.348: [GC (Allocation Failure) 0.348: [ParNew (promotion failed): 8133K->8917K(9216K), 0.0033605 secs]0.351: [CMS: 8194K->6902K(10240K), 0.0070997 secs] 12229K->6902K(19456K), [Metaspace: 3213K->3213K(1056768K)], 0.0106191 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
Heap
par new generation total 9216K, used 2130K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
eden space 8192K, 26% used [0x00000000fec00000, 0x00000000fee14930, 0x00000000ff400000)
from space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
concurrent mark-sweep generation total 10240K, used 6902K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
Metaspace used 3220K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 350K, capacity 388K, committed 512K, reserved 1048576K

  1. minor gc前,由于老年代空闲空间 6M 大于之前晋升对象大小4M,先进行minor gc
  2. 存活对象 array2 ~ 5都还存活着,并且 3个2M的对象都大于 survivor 区1M,需要直接晋升到老年代,可是老年代只有6M,array2和array3 先晋升到老年代(此时老年代 8M+),然后array4晋升的时候就放不下了 => promotion failed 晋升失败需要进行full gc, 回收掉 array1=4M, 然后放下晋升的 array4、 array5

你可能感兴趣的:(实验:老年代空闲空间放不下minorgc晋升的对象发生fullgc)