垃圾回收器

一、 分类
在这里插入图片描述
1、串行垃圾回收器(Serial) :
它为单线程环境设计并且只使用一个线程进行垃圾回收,会暂停所有的用户线程。所以不适合服务器环境
2、并行垃圾回收器(Parallel)
多个垃圾回收线程并行工作,此时用户线程是暂停的,适用于科学计算/大数据处理等弱交互场景
3、并发垃圾回收器(CMS)
用户线程和垃圾收集线程同时执行(不一定是并行,可能交替执行),不需要停顿用户线程 ,互联网公司多用它,适用于对响应时间有要求的场景
4、G1垃圾回收器
G1垃圾回收器将堆内存分割成不同的区域然后并发的对其进行垃圾回收

二、查看默认垃圾回收器
在这里插入图片描述
三、垃圾回收器名称如下:
UseSerialGC,UseParallelGC,UseConcMarkSweepGC,UseParNewGC,UseParallelOldGC,UseG1GC

GcDetail

[Full GC (System.gc()) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 628K->1046K(7168K)] 1132K->1046K(9728K), [Metaspace: 3977K->3977K(1056768K)], 0.0138494 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 

名词解释:
DefNew :      Default New Generation   
Tenured:      old
ParNew:       Parallel New Generation
PSYoungGen:   Parallel Scavenge
ParOldGen:    Parallel Old Generation

四、分代垃圾回收策略
在这里插入图片描述
在这里插入图片描述

1、新生代
1)串行GC(Serial)/(Serial Coping) 新生代和老年代 1对1
在这里插入图片描述
在这里插入图片描述

-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC     (DefNew+Tenured)

[GC (Allocation Failure) [DefNew: 2723K->319K(3072K), 0.0047526 secs] 2723K->1202K(9920K), 0.0048132 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Allocation Failure) [Tenured: 6847K->6847K(6848K), 0.0249263 secs] 9919K->9919K(9920K), [Metaspace: 3463K->3463K(1056768K)], 0.0249776 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
Heap
 def new generation   total 3072K, used 103K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
  eden space 2752K,   3% used [0x00000000ff600000, 0x00000000ff619ec0, 0x00000000ff8b0000)
  from space 320K,   0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
  to   space 320K,   0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
 tenured generation   total 6848K, used 999K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
   the space 6848K,  14% used [0x00000000ff950000, 0x00000000ffa49fd8, 0x00000000ffa4a000, 0x0000000100000000)
 Metaspace       used 3493K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 382K, capacity 388K, committed 512K, reserved 1048576K
  1. 并行GC(ParNew) 新生代并行,老年代串行 新生代和老年代 N对1
    在这里插入图片描述
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParNewGC     (ParNew+Tenured)
[GC (Allocation Failure) [ParNew: 3072K->3072K(3072K), 0.0000210 secs][Tenured: 5406K->6847K(6848K), 0.0248055 secs] 8478K->8052K(9920K), [Metaspace: 3464K->3464K(1056768K)], 0.0248932 secs] [Times: user=0.02 sys=0.00, real=0.03 secs] 
[Full GC (Allocation Failure) [Tenured: 6848K->6848K(6848K), 0.0269571 secs] 9919K->9919K(9920K), [Metaspace: 3464K->3464K(1056768K)], 0.0270080 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
Heap
 par new generation   total 3072K, used 103K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
  eden space 2752K,   3% used [0x00000000ff600000, 0x00000000ff619de0, 0x00000000ff8b0000)
  from space 320K,   0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
  to   space 320K,   0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
 tenured generation   total 6848K, used 989K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
   the space 6848K,  14% used [0x00000000ff950000, 0x00000000ffa476d0, 0x00000000ffa47800, 0x0000000100000000)
 Metaspace       used 3495K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 382K, capacity 388K, committed 512K, reserved 1048576K
  1. 并行回收GC(Parallel)/(Parallel Scavenge) 新生代并行,老年代也并行 新生代和老年代 N对N

在这里插入图片描述
在这里插入图片描述

-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParallelGC     (PSYoungGen+ParOldGen)
[GC (Allocation Failure) [PSYoungGen: 2528K->512K(2560K)] 6302K->6311K(9728K), 0.0053558 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7167K->7138K(7168K)] 9215K->9186K(9728K), [Metaspace: 3503K->3503K(1056768K)], 0.0617291 secs] [Times: user=0.13 sys=0.00, real=0.06 secs] 
Heap
 PSYoungGen      total 2560K, used 48K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 2% used [0x00000000ffd00000,0x00000000ffd0c0c8,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 661K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 9% used [0x00000000ff600000,0x00000000ff6a5558,0x00000000ffd00000)
 Metaspace       used 3533K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 386K, capacity 388K, committed 512K, reserved 1048576K
------------------------------------
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseParallelOldGC  (PSYoungGen+ParOldGen)
  与 UseParallelGC 相互激活
[GC (Allocation Failure) [PSYoungGen: 2536K->504K(2560K)] 4528K->4374K(9728K), 0.0054715 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7074K->7074K(7168K)] 9122K->9122K(9728K), [Metaspace: 3463K->3463K(1056768K)], 0.0539333 secs] [Times: user=0.13 sys=0.00, real=0.05 secs] 
Heap
 PSYoungGen      total 2560K, used 48K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 2% used [0x00000000ffd00000,0x00000000ffd0c070,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 667K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 9% used [0x00000000ff600000,0x00000000ff6a6f08,0x00000000ffd00000)
 Metaspace       used 3608K, capacity 4506K, committed 4864K, reserved 1056768K
  class space    used 391K, capacity 394K, committed 512K, reserved 1048576K
-----------------
 不加默认就是UseParallelGC
  -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags        (PSYoungGen+ParOldGen)

2、老年代
1) 串行回收GC(Serial Old)/(Serial MSC)
在这里插入图片描述
2) 并行GC(Parallel Old)/(Parallel MSC)
在这里插入图片描述
3) 并发标记清除GC(CMS)
在这里插入图片描述
步骤: 1、初始标记(CMS initial mark)
2、并发标记(CMS concurrent mark)和用户线程一起
进行GC Roots跟踪的过程,和用户线程一起工作,不需要暂停工作线程。主要标记过程,标记全部对象
3、重新标记(CMS remark)
为了修正在并发标记期间,因用户程序继续运行而导致标记产生变动的那一部分对象的标记记录,仍然需要暂停所有的工作线程
由于并发标记时,用户线程依然运行,因此在正式清理前,再做修正
4、并发清除(CMS concurrent sweep)和用户线程一起
清除GC Roots不可达对象,和用户线程一起工作,不需要暂停工作线程。基于标记结果,直接清理对象
由于耗时最长的并发标记和并发清除过程中,垃圾收集线程可以和用户现在一起并发工作,所以总体上来看CMS收集器的内存回
收和用户线程是一起并发的执行

优点:并发收集低停顿
缺点:并发执行,对CPU资源压力大
   

 -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseConcMarkSweepGC     (ParNew+CMS)

[GC (Allocation Failure) [ParNew: 2730K->320K(3072K), 0.0062604 secs] 2730K->1217K(9920K), 0.0063458 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 
 [Full GC (Allocation Failure) [CMS: 6848K->6847K(6848K), 0.0387146 secs] 9919K->9901K(9920K), [Metaspace: 3463K->3463K(1056768K)], 0.0387660 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
Heap
 par new generation   total 3072K, used 76K [0x00000000ff600000, 0x00000000ff950000, 0x00000000ff950000)
  eden space 2752K,   2% used [0x00000000ff600000, 0x00000000ff613060, 0x00000000ff8b0000)
  from space 320K,   0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ff950000)
  to   space 320K,   0% used [0x00000000ff8b0000, 0x00000000ff8b0000, 0x00000000ff900000)
 concurrent mark-sweep generation total 6848K, used 662K [0x00000000ff950000, 0x0000000100000000, 0x0000000100000000)
 Metaspace       used 3493K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 382K, capacity 388K, committed 512K, reserved 1048576K
--------------
1[GC (CMS Initial Mark) [1 CMS-initial-mark: 6847K(6848K)] 9901K(9920K), 0.0022449 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
2[CMS-concurrent-mark-start]
[Full GC (Allocation Failure) [CMS[CMS-concurrent-mark: 0.008/0.009 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
3[GC (CMS Final Remark) [YG occupancy: 2324 K (3072 K)][Rescan (parallel) , 0.0021250 secs][weak refs processing, 0.0000257 secs][class unloading, 0.0003732 secs][scrub symbol table, 0.0008206 secs][scrub string table, 0.0016035 secs][1 CMS-remark: 5381K(6848K)] 7705K(9920K), 0.0051164 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 
4[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.005/0.005 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

五、如何选择垃圾收集器
在这里插入图片描述
在这里插入图片描述

六、G1

-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseG1GC
[GC pause (G1 Evacuation Pause) (young) (to-space exhausted), 0.0093763 secs]
   [Parallel Time: 4.0 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 842.1, Avg: 842.5, Max: 843.7, Diff: 1.5]
      [Ext Root Scanning (ms): Min: 0.1, Avg: 0.7, Max: 1.0, Diff: 0.9, Sum: 2.9]
      [Update RS (ms): Min: 0.0, Avg: 0.2, Max: 0.9, Diff: 0.9, Sum: 0.9]
         [Processed Buffers: Min: 0, Avg: 0.3, Max: 1, Diff: 1, Sum: 1]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Object Copy (ms): Min: 1.4, Avg: 2.6, Max: 3.1, Diff: 1.6, Sum: 10.4]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Termination Attempts: Min: 1, Avg: 25.5, Max: 43, Diff: 42, Sum: 102]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
      [GC Worker Total (ms): Min: 2.4, Avg: 3.6, Max: 4.0, Diff: 1.5, Sum: 14.3]
      [GC Worker End (ms): Min: 846.1, Avg: 846.1, Max: 846.1, Diff: 0.0]
   [Code Root Fixup: 0.0 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.0 ms]
   [Other: 5.3 ms]
      [Evacuation Failure: 5.2 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.0 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.0 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.0 ms]
   [Eden: 1024.0K(1024.0K)->0.0B(1024.0K) Survivors: 0.0B->1024.0K Heap: 8780.9K(10.0M)->9804.9K(10.0M)]
 [Times: user=0.02 sys=0.00, real=0.01 secs] 
-----------
Heap
 garbage-first heap   total 10240K, used 658K [0x00000000ff600000, 0x00000000ff700050, 0x0000000100000000)
  region size 1024K, 1 young (1024K), 0 survivors (0K)
 Metaspace       used 3495K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 382K, capacity 388K, committed 512K, reserved 1048576K

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
特点
在这里插入图片描述
底层原理
Region区域化垃圾收集器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
回收步骤
在这里插入图片描述
在这里插入图片描述
4步过程
在这里插入图片描述
和CMS相比的优势
在这里插入图片描述
g1参考文档
https://www.jianshu.com/p/333147dc741a
https://blog.csdn.net/fedorafrog/article/details/104503829/

g1和cms区别
https://blog.csdn.net/shlgyzl/article/details/95041113
https://www.cnblogs.com/rayduan/p/xrq730.html
https://www.jianshu.com/p/24a884fa3977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值