欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MinorGC 与 Full GC 区别

程序员文章站 2022-03-02 22:09:14
...

新生代GC(Minor GC):是指在新生代的垃圾收集动作,因为java对象大多都具备朝生夕死的特性,所以Minor GC 非常频繁,一般回收速度也比较快。

代码实例:

package com.Studay.GC;

public class MinorGC {

	public static final int _1MB = 1024*1024;
	
	/**
	 * VM 参数: -verbose:gc -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails
	 * -XX:SurvivorRatio=8
	 * 
	 * 参数意义:将堆最大与最小设置为20m,其中年轻代分为10m
	 * 
	 */
	
	public static void main(String[] args) {
		byte[] allocation1,allocation2,allocation3,allocation4;
		
		allocation1 = new byte[_1MB * 2];
		allocation2 = new byte[_1MB * 2];
		allocation3 = new byte[_1MB * 2];
		allocation4 = new byte[_1MB * 4]; //出现一次Minor GC
	}
}

代码讲解:

执行分配allocation的时发生了一次Minor GC ;


[GC (Allocation Failure) [DefNew: 7127K->518K(9216K), 0.0030483 secs] 7127K->6662K(19456K), 0.0030837 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4696K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  51% used [0x00000000fec00000, 0x00000000ff014930, 0x00000000ff400000)
  from space 1024K,  50% used [0x00000000ff500000, 0x00000000ff581828, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 tenured generation   total 10240K, used 6144K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  60% used [0x00000000ff600000, 0x00000000ffc00030, 0x00000000ffc00200, 0x0000000100000000)
 Metaspace       used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 281K, capacity 386K, committed 512K, reserved 1048576K


老年代GC(Major GC / FullGC):是指发生在老年代的GC,