为什么?完整的gc发生在两次次要gc之前?

时间:2019-03-11 13:06:45

标签: java java-8 garbage-collection

public class Test1 {

    public static final int _1MB = 1024 * 1024;

    public static void main(String agrs[]) {
            gc();
            System.out.println("hello world");
    }

    private static void gc() {
            byte[] bytes = new byte[3*_1MB];
            byte[] bytes1 = new byte[_1MB];
            bytes = null;
            byte[] bytes2 = new byte[18*_1MB];
    }
}

JDK1.8

-Xms25M -Xmx25M -XX:NewRatio=4 -XX:MaxTenuringThreshold=1 -XX:+UseParallelOldGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps

输出:

0.080: [GC (Allocation Failure) [PSYoungGen: 3677K->496K(4608K)] 3677K->3576K(26112K), 0.0031388 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
0.084: [GC (Allocation Failure) [PSYoungGen: 1520K->0K(4608K)] 4600K->4416K(26112K), 0.0013931 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
0.085: [GC (Allocation Failure) [PSYoungGen: 0K->0K(4608K)] 4416K->4416K(26112K), 0.0009730 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
0.086: [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(4608K)] [ParOldGen: 4416K->1298K(21504K)] 4416K->1298K(26112K), [Metaspace: 2635K->2635K(1056768K)], 0.0027873 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
hello world

为什么要在两次次要GC之前先进行Full GC?

1 个答案:

答案 0 :(得分:1)

垃圾回收的全部目的是进行许多次要回收,而很少进行主要回收。发生的情况当然是实现细节,但是在stackoverflow上有很多文章可以阐明。

您看到的是绝对正常的,只是健康的垃圾收集过程的一部分。

相关问题