不同垃圾收集器种类的GC日志

jvm的垃圾收集器分为新生代和老年代的收集器,使用的算法不一样
新生代采用复制算法,就是保留一个eden区,两个survivor区,默认是8:1:1
当eden区满了,就执行minorGC将对象回收,幸存的对象就被复制到to survivor区,
然后eden区另一个survivor区清空。 下一次回收重复执行以上。
复制到survivor区的时候发现装不下,会被放到老年代去

当来了一个对象,eden区装不下,执行minorGC之后还是装不下,就会被放到老年代。
或者一个对象在新生代,默认是连续经过15次垃圾回收之后 还活着,就晋升到老年代。

总之就是,对象优先在eden区分配

老年代的算法,则是标记-清除算法 或者标记-整理算法
当老年代满了之后 ,执行full gc 。

jvm提供的垃圾收集器组合如下:

-XX:+UseSerialGC,虚拟机运行在Client模式下的默认值,Serial+Serial Old。
Serial单线程收集器 Serial Old单线程老年代收集器
缺点是单线程,gc时候停顿时间长

-XX:+UseParNewGC,ParNew+Serial Old
ParNew 多线程并发收集器 优点是清除的阶段可以和用户线程一起执行,停顿时间短,标记阶段还是要暂停整个应用

-XX:+UseConcMarkSweepGC,ParNew+CMS+Serial Old。

ParNew 多线程新生代收集器,CMS并发老年代收集器(标记清除算法) Serial Old 备用的单线程老年代收集器, 因为cms有失败的情况,清除线程和用户线程同时执行,用户线程可能在这个阶段又产生新的垃圾,如果这个垃圾过多过大导致老年代空间不够,这个时候又有新的对象要放进来,这个时候就会让Serial Old接手,暂停整个应用,然后执行标记-整理算法。

-XX:+UseParallelGC,虚拟机运行在Server模式下的默认值,Parallel Scavenge+Serial Old(PS Mark Sweep)。

Parallel Scavenge 以吞吐量为优先的垃圾收集器 ,可以自动设置新生代的各个区的比率 ,控制收集的吞吐量

-XX:+UseParallelOldGC,Parallel Scavenge+Parallel Old。
Parallel Old 是Parallel Scavenge的老年代版本

-XX:+UseG1GC,G1+G1。
G1 ,别具一格的垃圾收集器,将整个管理的堆都划分为相同的region,
然后哪个region快要满的时候,就收集哪个region, 避免了全局回收,也可以和用户线程并行执行,region里面是标记-整理算法,两个region之间是复制算法,无内存碎片,低停顿。

jvm参数如下
不同垃圾收集器种类的GC日志

最大堆和最小堆均为20mb, survivorRatio=8 表示新生代的eden区和survivor区比为8:1
但是survivor区有两个
新生代为8mb 两个survivor区为1

测试代码如下

public class GCtest {

    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) {
        testAllocation();
    }
}

-XX:+UseSerialGC ( Serial+Serial Old) ( 头部标示def new + tenured)

//发生g c 分配失败 新生代的从8027K大小,回收垃圾后为428K(9216K 总大小) 用时 0.0059315 secs
//总空间为 垃圾回收前8027K-> 垃圾回收后6572K( 总空间大小 19456K),
[GC (Allocation Failure) [DefNew: 8027K->428K(9216K), 0.0059315 secs] 8027K->6572K(19456K), 0.0059604 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

//堆中
Heap
//新生代 (垃圾收集器为Serial )
//总共9216K 已经使用4606k
 def new generation   total 9216K, used 4606K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
 //eden区8192K,使用了51%
  eden space 8192K,  51% used [0x00000007bec00000, 0x00000007bf014930, 0x00000007bf400000)
  //第一个survivor区使用了41%  第二个使用了0
  from space 1024K,  41% used [0x00000007bf500000, 0x00000007bf56b1c0, 0x00000007bf600000)
  to   space 1024K,   0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)

//老年代 (收集器为Serial Old)
//总共10240k 已经使用6144k
 tenured generation   total 10240K, used 6144K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
 //使用了60%
   the space 10240K,  60% used [0x00000007bf600000, 0x00000007bfc00030, 0x00000007bfc00200, 0x00000007c0000000)
 Metaspace       used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 365K, capacity 388K, committed 512K, reserved 1048576K

过程如下:

allocation1 = new byte[ 2*_1MB];
allocation1被分配到新生代的e den区,占用了2M b ,eden区剩余6m b

    allocation2 = new byte[2 * _1MB];
     allocation2被分配到新生代的e den区,占用了2M b ,eden区剩余4m b
    allocation3 = new byte[2 * _1MB];
         allocation3被分配到新生代的e den区,占用了2M b ,eden区剩余2m b
    allocation4 = new byte[4 * _1MB];
       allocation3被分配到新生代的e den区,占用了4M b ,发现e den区装不下,触发minorGC
       将allocation1  allocation2  allocation3 往su rvivor区放,发现survivor区放不下,于是都放到了老年代,老年代使用了6m b,  然后将allocation4放到了新生代的e den区
       最终 新生代的e den 区4m b,老年代6m b

-XX:+UseParNewGC (ParNew+Serial Old) ( 头部标示par new + tenured)

[GC (Allocation Failure) [ParNew: 8191K->432K(9216K), 0.0044039 secs] 8191K->6576K(19456K), 0.0044263 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Heap
 par new generation   total 9216K, used 4694K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
  eden space 8192K,  52% used [0x00000007bec00000, 0x00000007bf029918, 0x00000007bf400000)
  from space 1024K,  42% used [0x00000007bf500000, 0x00000007bf56c0e0, 0x00000007bf600000)
  to   space 1024K,   0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)
 tenured generation   total 10240K, used 6144K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
   the space 10240K,  60% used [0x00000007bf600000, 0x00000007bfc00030, 0x00000007bfc00200, 0x00000007c0000000)
 Metaspace       used 3293K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 363K, capacity 388K, committed 512K, reserved 1048576K


-XX:+UseConcMarkSweepGC (ParNew+CMS+Serial Old)
( 头部标示par new + concurrent mark-sweep)


[GC (Allocation Failure) [ParNew: 8027K->453K(9216K), 0.0057184 secs] 8027K->6599K(19456K), 0.0058239 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Heap
 par new generation   total 9216K, used 4631K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
  eden space 8192K,  51% used [0x00000007bec00000, 0x00000007bf014930, 0x00000007bf400000)
  from space 1024K,  44% used [0x00000007bf500000, 0x00000007bf571568, 0x00000007bf600000)
  to   space 1024K,   0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)
  //表示是cm s收集器
 concurrent mark-sweep generation total 10240K, used 6146K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
 Metaspace       used 3306K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 365K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

-XX:+UseParallelGC. (Parallel Scavenge+Serial Old(PS Mark Sweep))

( 头部标示PSYoungGen +ParOldGen)

Heap
 PSYoungGen      total 9216K, used 8192K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 8192K, 100% used [0x00000007bf600000,0x00000007bfe00000,0x00000007bfe00000)
  from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
  to   space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
 ParOldGen       total 10240K, used 4096K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
  object space 10240K, 40% used [0x00000007bec00000,0x00000007bf000010,0x00000007bf600000)
 Metaspace       used 3287K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 362K, capacity 388K, committed 512K, reserved 1048576K


-XX:+UseParallelOldGC (Parallel Scavenge+Parallel Old)
( 头部标示PSYoungGen + ParOldGen)

Heap
PSYoungGen      total 9216K, used 8192K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
 eden space 8192K, 100% used [0x00000007bf600000,0x00000007bfe00000,0x00000007bfe00000)
 from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
 to   space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen       total 10240K, used 4096K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
 object space 10240K, 40% used [0x00000007bec00000,0x00000007bf000010,0x00000007bf600000)
Metaspace       used 3292K, capacity 4496K, committed 4864K, reserved 1056768K
 class space    used 363K, capacity 388K, committed 512K, reserved 1048576K