自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java11 的 G1 垃圾收集器

開發(fā) 后端
作為 5 年以上工作經(jīng)驗的技術(shù)人員,或多或少在系統(tǒng)維護,系統(tǒng)保障,系統(tǒng)調(diào)優(yōu)遇到過上面的這幾個場景,你可能是通過重啟,調(diào)整一些JVM參數(shù)解決,如果大家需要深入的探究找到問題的原因,可以耐心看看下文我對 G1 的一些總結(jié)。

大家好,我是【運維開發(fā)故事公眾號】的 Java 程序員老鄭。JVM 是我們從事 Java 項目和開發(fā)都需要具備的非常底層的基礎(chǔ)知識。

首先我先拋出以下幾個問題:

  • 很多服務(wù)需要過一段時間重啟一次,如果不重啟系統(tǒng)就會越來越慢?
  • 突然一個中間件掛了一段時間過后,但是一些不相關(guān)的服務(wù)越來越卡,后面 OOM?
  • 上線一個功能過后,CPU 就飆升到 100%,但是服務(wù)還是正常運行?
  • 服務(wù)的某一個 CPU 出現(xiàn)有規(guī)律的,周期性的尖刺該如何解決?

作為 5 年以上工作經(jīng)驗的技術(shù)人員,或多或少在系統(tǒng)維護,系統(tǒng)保障,系統(tǒng)調(diào)優(yōu)遇到過上面的這幾個場景,你可能是通過重啟,調(diào)整一些 jvm 參數(shù)解決,如果大家需要深入的探究找到問題的原因,可以耐心看看下文我對 G1 的一些總結(jié)。

本文講哪些東西?

  • 堆布局(以 Region 為基礎(chǔ)劃分:新生代(Eden 區(qū)、Survivor 區(qū))、年老代、Humongous 區(qū)域)
  • 垃圾收集周期
  • GC 運作過程:初始標(biāo)記、并發(fā)標(biāo)記、最終標(biāo)記、篩選回收
  • GC 類型:Minor GC、Full GC 、Mixed GC
  • CSet (年輕代需要收集的 Region 集合就是 CSet)
  • 跨代引用
  • 停頓預(yù)測模型
  • GC 日志分析

G1 內(nèi)存堆布局

G1 的英文全稱是 Garbagge First,是一個有分代,按照 Region 的方式進行內(nèi)存布局的垃圾收集器。

上圖,我將一些 Region 標(biāo)明了 H,它代表Humongous,這表示這些 Region 存儲的是巨大對象(humongous object,H-obj),即大小大于等于 region 一半的對象。H-obj 有如下幾個特征:

  • H-obj 直接分配到了old gen,防止了反復(fù)拷貝移動。
  • H-obj 在 global concurrent marking 階段的 cleanup 和 full GC階段回收。
  • 在分配 H-obj 之前先檢查是否超過 initiating heap occupancy percent 和 the marking threshold, 如果超過的話,就啟動 global concurrent marking,為的是提早回收,防止 evacuation failures 和 full GC。

GC 類型

  1. Young GC,垃圾收集范圍:年輕代區(qū)域 + 大對象區(qū)。
  2. Mixed GC,垃圾收集范圍:年輕代區(qū)域 + 老年區(qū) + 大對象區(qū)。
  3. Full GC,垃圾收集范圍:年輕代區(qū)域 + 老年區(qū) + 大對象區(qū) + 元空間。

Collection Set (收集區(qū)域)

Collection Set 就是我們垃圾收集器的一個區(qū)域,在不同的垃圾回收階段,會有不同的區(qū)域。

  • Young GC, 垃圾收集區(qū)域包括:年輕代區(qū)域 + 大對象區(qū)。
  • Mixed GC, 垃圾收集區(qū)域包括:年輕代區(qū)域 + 老年區(qū) + 大對象區(qū)。

跨代引用

Young GC 主要是清理,新生代中的對象,我們知道整個堆空間包括老年代,新生代,我們在 Young GC 過程中會去找 GCRoots 然后判斷對象是是否可達(dá), 如果不可達(dá),如果可達(dá)就標(biāo)記。如果對于老年代中引用新生代的對象,我們?nèi)绻页鰜砭途托枰獙夏甏M行全掃描,這樣是不太現(xiàn)實的。所以 G1 通過記憶集的形式記錄了老年代對新生代的引用。具體在 G1 中通過 CarTable 來實現(xiàn)記憶集。

RSet(記憶集)

記錄了其它 Region 中的對象到 Region 的引用。RSet 的價值在于使得垃圾回收不需要掃描整個堆,能夠快速定位到真正引用它的堆對象地址。ReSet 本身就是一個 Hash 表,存儲在新生代的每個 Region 中。但是存儲需要消耗空間,多的能達(dá)到百分之 20。因此G1對內(nèi)存的空間要求較高(小空間沒資本玩),空間越大性能越彪悍。

CardTable (卡表)

由于新生代GC時,需要掃描整個old區(qū),效率非常低。所以old區(qū)就是用卡表的方式進行一次邏輯分區(qū)。一般一頁卡表的大小是2的n次冪。每一個區(qū)域也是用Key,Value結(jié)構(gòu)進行記錄。每一區(qū)域記錄為Key不重復(fù),Value則記錄這片區(qū)域的老年代對象與新生代對象是否存在引用關(guān)系,存在則標(biāo)記為1,否則為0。記錄完畢后把value為1的key作為ReSet的key進行記錄,并且ReSet的value存儲引用,從而提高跨代引用的查詢效率。

停頓預(yù)測模型

所有的預(yù)測都是基于歷史的擬合,HotSpot使用了基于方差與標(biāo)準(zhǔn)差的技術(shù)。參考:https://sdww2348115.github.io/jvm/g1/PausePredictionModel。

G1 垃圾收集周期

圖片來源 Oracle 官網(wǎng)

G1 有兩個階段,它會在這兩個階段往返,分別是 Young-only,Space Reclamation.

  • Young-only 包含一系列的操作,如果長期存活的對象會逐漸轉(zhuǎn)移到 Old gen。
  • Space Reclamation G1 會遞進地回收 Old gen 的空間,同時也處理 Young region。

圖是來自 Oracle 上對 GC 周期的描述,實心圓都表示一次 GC 停頓

  • 藍(lán)色 Young-only。
  • 黃色 標(biāo)記過程的停頓。
  • 紅色 Mixed GC 停頓。

在幾次 GC 后,Old gen 的對象占有比超過了 InitiatingHeapOccupancyPercent (簡稱為IHOP,默認(rèn)值為45,這個值是啟動并發(fā)標(biāo)記的閾值,當(dāng)老年代使用內(nèi)存占用堆內(nèi)存的45%啟動并發(fā)標(biāo)記。如果該區(qū)域過大,可能會導(dǎo)致mixed gc跟不上內(nèi)存分配的速度從而導(dǎo)致full gc ),gc 就會進入并發(fā)標(biāo)記準(zhǔn)備 (Concurrent Mark)。

  • G1 在每一次 Young 回收中都會查找活對象 (有引用的對象)。
  • G1 在 old region 并發(fā)查找存活對象。
  • 是 Concurrent Marking。
  • 可能花費很長時間。
  • 不會停止 Java 應(yīng)用。
  • G1 沒有活對象的引用信息是不能進行垃圾回收的。
  • Mixed GC 依賴 Concurrent Mark。

回到 Full GC,從上面簡單分析得出,F(xiàn)ull GC 發(fā)生是沒有足夠的 free region,如果堆是足夠大的,Mixed gc 沒有回收足夠的 old region,或者 concurrent mark 沒法及時完成,都可能會導(dǎo)致 full gc。

GC 日志分析

下面是網(wǎng)上找的一個 GC 日志案例,解析如下(配合 G1 垃圾收集周期結(jié)合來看):

[gc,start      ] GC(44265) Pause Young (Normal) (G1 Evacuation Pause)
[gc,task       ] GC(44265) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44265)   Pre Evacuate Collection Set: 0.1ms
[gc,phases     ] GC(44265)   Evacuate Collection Set: 101.8ms
[gc,phases     ] GC(44265)   Post Evacuate Collection Set: 3.2ms
[gc,phases     ] GC(44265)   Other: 2.7ms
[gc,heap       ] GC(44265) Eden regions: 1850->0(1851)
[gc,heap       ] GC(44265) Survivor regions: 70->69(240)
[gc,heap       ] GC(44265) Old regions: 766->768
[gc,heap       ] GC(44265) Humongous regions: 20->19
[gc,metaspace  ] GC(44265) Metaspace: 193280K->193280K(1230848K)
[gc            ] GC(44265) Pause Young (Normal) (G1 Evacuation Pause) 21642M->6843M(25600M) 107.561ms
[gc,cpu        ] GC(44265) User=1.31s Sys=0.00s Real=0.11s

[gc,start      ] GC(44266) Pause Young (Normal) (G1 Evacuation Pause)
[gc,task       ] GC(44266) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44266)   Pre Evacuate Collection Set: 0.1ms
[gc,phases     ] GC(44266)   Evacuate Collection Set: 99.8ms
[gc,phases     ] GC(44266)   Post Evacuate Collection Set: 3.3ms
[gc,phases     ] GC(44266)   Other: 2.7ms
[gc,heap       ] GC(44266) Eden regions: 1851->0(1854)
[gc,heap       ] GC(44266) Survivor regions: 69->66(240)
[gc,heap       ] GC(44266) Old regions: 768->772
[gc,heap       ] GC(44266) Humongous regions: 20->19
[gc,metaspace  ] GC(44266) Metaspace: 193280K->193280K(1230848K)
[gc            ] GC(44266) Pause Young (Normal) (G1 Evacuation Pause) 21659M->6848M(25600M) 105.713ms
[gc,cpu        ] GC(44266) User=1.29s Sys=0.01s Real=0.10s

[gc,start      ] GC(44267) Pause Young (Normal) (G1 Evacuation Pause)
[gc,task       ] GC(44267) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44267)   Pre Evacuate Collection Set: 0.1ms  //初始標(biāo)記,查找 gc root 
[gc,phases     ] GC(44267)   Evacuate Collection Set: 89.8ms     //并發(fā)標(biāo)記
[gc,phases     ] GC(44267)   Post Evacuate Collection Set: 3.5ms //清理工作
[gc,phases     ] GC(44267)   Other: 2.7ms
[gc,heap       ] GC(44267) Eden regions: 1854->0(1856)
[gc,heap       ] GC(44267) Survivor regions: 66->64(240)
[gc,heap       ] GC(44267) Old regions: 772->775
[gc,heap       ] GC(44267) Humongous regions: 20->19
[gc,metaspace  ] GC(44267) Metaspace: 193280K->193280K(1230848K)
[gc            ] GC(44267) Pause Young (Normal) (G1 Evacuation Pause) 21688M->6859M(25600M) 95.891ms
[gc,cpu        ] GC(44267) User=1.16s Sys=0.00s Real=0.10s

[gc,start      ] GC(44268) Pause Young (Normal) (G1 Evacuation Pause)                 // Young GC
[gc,task       ] GC(44268) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44268)   Pre Evacuate Collection Set: 0.1ms
[gc,phases     ] GC(44268)   Evacuate Collection Set: 100.5ms
[gc,phases     ] GC(44268)   Post Evacuate Collection Set: 3.8ms
[gc,phases     ] GC(44268)   Other: 2.8ms
[gc,heap       ] GC(44268) Eden regions: 1856->0(1855)
[gc,heap       ] GC(44268) Survivor regions: 64->65(240)
[gc,heap       ] GC(44268) Old regions: 775->777
[gc,heap       ] GC(44268) Humongous regions: 20->19
[gc,metaspace  ] GC(44268) Metaspace: 193280K->193280K(1230848K)
[gc            ] GC(44268) Pause Young (Normal) (G1 Evacuation Pause) 21715M->6876M(25600M) 107.037ms
[gc,cpu        ] GC(44268) User=1.30s Sys=0.00s Real=0.11s

[gc,start      ] GC(44269) Pause Young (Concurrent Start) (G1 Humongous Allocation)  // 并發(fā)階段
[gc,task       ] GC(44269) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44269)   Pre Evacuate Collection Set: 0.6ms
[gc,phases     ] GC(44269)   Evacuate Collection Set: 90.9ms
[gc,phases     ] GC(44269)   Post Evacuate Collection Set: 3.2ms
[gc,phases     ] GC(44269)   Other: 2.9ms
[gc,heap       ] GC(44269) Eden regions: 1519->0(1855)
[gc,heap       ] GC(44269) Survivor regions: 65->65(240)
[gc,heap       ] GC(44269) Old regions: 777->777
[gc,heap       ] GC(44269) Humongous regions: 19->19
[gc,metaspace  ] GC(44269) Metaspace: 193280K->193280K(1230848K)
[gc            ] GC(44269) Pause Young (Concurrent Start) (G1 Humongous Allocation) 19024M->6883M(25600M) 97.391ms
[gc,cpu        ] GC(44269) User=1.16s Sys=0.01s Real=0.10s

[gc            ] GC(44270) Concurrent Cycle                                          // 完成 clearup
[gc,marking    ] GC(44270) Concurrent Clear Claimed Marks
[gc,marking    ] GC(44270) Concurrent Clear Claimed Marks 0.562ms
[gc,marking    ] GC(44270) Concurrent Scan Root Regions
[gc,marking    ] GC(44270) Concurrent Scan Root Regions 719.931ms
[gc,marking    ] GC(44270) Concurrent Mark (280799.914s)
[gc,marking    ] GC(44270) Concurrent Mark From Roots
[gc,task       ] GC(44270) Using 3 workers of 3 for marking
[gc,marking    ] GC(44270) Concurrent Mark From Roots 2268.905ms
[gc,marking    ] GC(44270) Concurrent Preclean
[gc,marking    ] GC(44270) Concurrent Preclean 3.078ms
[gc,marking    ] GC(44270) Concurrent Mark (280799.914s, 280802.186s) 2272.068ms
[gc,start      ] GC(44270) Pause Remark
[gc,stringtable] GC(44270) Cleaned string and symbol table, strings: 87967 processed, 92 removed, symbols: 442773 processed, 13 removed
[gc            ] GC(44270) Pause Remark 13740M->13740M(25600M) 32.599ms
[gc,cpu        ] GC(44270) User=0.29s Sys=0.00s Real=0.04s
[gc,marking    ] GC(44270) Concurrent Rebuild Remembered Sets            //重構(gòu)記憶集
[gc,marking    ] GC(44270) Concurrent Rebuild Remembered Sets 1906.792ms
[gc,start      ] GC(44270) Pause Cleanup
[gc            ] GC(44270) Pause Cleanup 18019M->18019M(25600M) 0.782ms
[gc,cpu        ] GC(44270) User=0.00s Sys=0.01s Real=0.00s
[gc,marking    ] GC(44270) Concurrent Cleanup for Next Mark
[gc,marking    ] GC(44270) Concurrent Cleanup for Next Mark 25.530ms
[gc            ] GC(44270) Concurrent Cycle 4963.833ms

[gc,start      ] GC(44271) Pause Young (Prepare Mixed) (G1 Evacuation Pause)  // Space Reclamation 階段了,多個 Mixed GC 會進行
[gc,task       ] GC(44271) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44271)   Pre Evacuate Collection Set: 0.1ms
[gc,phases     ] GC(44271)   Evacuate Collection Set: 102.6ms
[gc,phases     ] GC(44271)   Post Evacuate Collection Set: 3.7ms
[gc,phases     ] GC(44271)   Other: 3.9ms
[gc,heap       ] GC(44271) Eden regions: 1855->0(98)
[gc,heap       ] GC(44271) Survivor regions: 65->62(240)
[gc,heap       ] GC(44271) Old regions: 777->778
[gc,heap       ] GC(44271) Humongous regions: 21->19
[gc,metaspace  ] GC(44271) Metaspace: 193271K->193271K(1230848K)
[gc            ] GC(44271) Pause Young (Prepare Mixed) (G1 Evacuation Pause) 21739M->6869M(25600M) 110.034ms
[gc,cpu        ] GC(44271) User=1.32s Sys=0.01s Real=0.10s

[gc,start      ] GC(44272) Pause Young (Mixed) (G1 Evacuation Pause)
[gc,task       ] GC(44272) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44272)   Pre Evacuate Collection Set: 0.4ms
[gc,phases     ] GC(44272)   Evacuate Collection Set: 150.8ms
[gc,phases     ] GC(44272)   Post Evacuate Collection Set: 3.2ms
[gc,phases     ] GC(44272)   Other: 2.3ms
[gc,heap       ] GC(44272) Eden regions: 98->0(149)
[gc,heap       ] GC(44272) Survivor regions: 62->11(20)
[gc,heap       ] GC(44272) Old regions: 778->547
[gc,heap       ] GC(44272) Humongous regions: 19->19
[gc,metaspace  ] GC(44272) Metaspace: 193271K->193271K(1230848K)
[gc            ] GC(44272) Pause Young (Mixed) (G1 Evacuation Pause) 7653M->4605M(25600M) 156.486ms
[gc,cpu        ] GC(44272) User=1.95s Sys=0.01s Real=0.15s
[gc,start      ] GC(44273) Pause Young (Mixed) (G1 Evacuation Pause)
[gc,task       ] GC(44273) Using 13 workers of 13 for evacuation
[gc,phases     ] GC(44273)   Pre Evacuate Collection Set: 0.2ms
[gc,phases     ] GC(44273)   Evacuate Collection Set: 122.9ms
[gc,phases     ] GC(44273)   Post Evacuate Collection Set: 2.0ms
[gc,phases     ] GC(44273)   Other: 3.1ms
[gc,heap       ] GC(44273) Eden regions: 149->0(1900)
[gc,heap       ] GC(44273) Survivor regions: 11->20(20)
[gc,heap       ] GC(44273) Old regions: 547->520
[gc,heap       ] GC(44273) Humongous regions: 19->19
[gc,metaspace  ] GC(44273) Metaspace: 193271K->193271K(1230848K)
[gc            ] GC(44273) Pause Young (Mixed) (G1 Evacuation Pause) 5797M->4468M(25600M) 128.036ms
[gc,cpu        ] GC(44273) User=1.57s Sys=0.01s Real=0.12s

上面是連續(xù)幾次 GC 的日志,可以對照著 GC 周期來看。

  • GC (44265) 是一次普通的 Young GC里面信息有各種 Region 的變化。

這里簡單說一下 humongous 對象的處理,humongous 對象在 G1 中是被特殊對待的,G1 只決定它們是否生存,回收他們占用的空間,從不會移動它們。

  • Young-Only 階段,humongous regions 可能會被回收。
  • Space-Reclamation,humongous regions 可能會被回收。
  • GC (44269) 開始進入并發(fā)階段。
  • GC (44270) 完成了 Cleanup,緊接著一個 Prepare Mixed GC (44271) 的垃圾收集,對應(yīng)周期虛線右邊的藍(lán)實心圓。
  • GC (44272) 之后就是 Space Reclamation 階段了,多個 Mixed GC 會進行。

JVM 性能監(jiān)控工具

我們可以通過以下幾種工具輔助分析 JVM 性能瓶頸:

綜合組件:

  • VisualVM
  • Glowroot
  • https://arthas.aliyun.com/。

thread dump 分析:

  • https://fastthread.io/。

gc 日志分析:

  • https://gceasy.io/gc-index.jsp。

heap dump 分析:

  • https://www.ibm.com/support/pages/ibm-heapanalyzer。
  • https://projects.eclipse.org/projects/tools.mat。

參考資料

【GC 停頓預(yù)測模型】

  • http://www.narihiro.info/g1gc-impl-book/scheduling.html。
  • https://sdww2348115.github.io/jvm/g1/PausePredictionModel。

【垃圾收集器執(zhí)行過程】

  • https://bugs.openjdk.org/browse/JDK-8295118。
  • https://my.oschina.net/u/4273516/blog/4550072。

【跨代引用】

  • https://blog.csdn.net/weixin_47184173/article/details/113627337。

【空閑時自動將Java堆內(nèi)存返回給操作系統(tǒng)】。

  • https://openjdk.org/jeps/346

【其他】

  • https://docs.oracle.com/javacomponents/jmc-5-4/jfr-runtime-guide/comline.htm#JFRUH197。
  • https://www.redhat.com/en/blog/part-1-introduction-g1-garbage-collector。
  • https://blog.csdn.net/qq_16500963/article/details/132133125。
  • http://cs.williams.edu/~dbarowy/cs334s18/assets/p37-detlefs.pdf。
  • https://tech.meituan.com/2016/09/23/g1.html。
  • https://hllvm-group.iteye.com/group/topic/44381。
責(zé)任編輯:姜華 來源: 運維開發(fā)故事
相關(guān)推薦

2021-08-15 18:59:13

垃圾收集器JDK

2015-06-17 14:10:52

OracleJava 9垃圾收集器

2015-07-29 10:28:59

JVM參數(shù)配置參數(shù)

2009-08-14 08:56:49

Java垃圾回收器G1

2022-07-25 10:15:29

垃圾收集器Java虛擬機

2009-07-24 09:41:45

Java 7 G1垃圾回收器

2011-07-21 14:54:26

java垃圾收集器

2024-04-29 08:20:40

Java后端Redlock

2020-05-14 13:39:19

Java 垃圾回收機制

2011-05-10 16:04:45

Java垃圾收集器

2017-09-21 14:40:06

jvm算法收集器

2009-10-30 10:47:48

VB.NET垃圾收集器

2024-12-30 08:03:08

2009-06-02 10:18:43

Java垃圾回收器垃圾回收器Java垃圾回收

2024-08-26 08:58:50

2022-02-25 08:01:34

CMS

2024-10-22 16:26:11

2022-04-19 11:25:31

JVMZGC垃圾收集器

2013-12-19 09:46:04

垃圾收集器

2024-03-27 10:27:35

延遲垃圾收集器
點贊
收藏

51CTO技術(shù)棧公眾號