G1垃圾收集器
G1是一個(gè)面向服務(wù)端的JVM垃圾收集器朵你,適用于多核處理器、大內(nèi)存容量的服務(wù)端系統(tǒng)。 它滿足短時(shí)間停頓的同時(shí)達(dá)到一個(gè)高的吞吐量澈蟆。從JDK 9開始,G1成為默認(rèn)的垃圾回收器卓研。
當(dāng)應(yīng)用有以下任何一種特性時(shí)非常適合用G1:
- Full GC持續(xù)時(shí)間太長(zhǎng)或者太頻繁
- 對(duì)象的創(chuàng)建速率和存活率變動(dòng)很大
- 應(yīng)用不希望停頓時(shí)間長(zhǎng)(長(zhǎng)于0.5s甚至1s)
G1對(duì)Heap的劃分
G1按固定大小把內(nèi)存劃分為很多小區(qū)域(region)趴俘,這個(gè)堆大概有2000多塊;在邏輯上奏赘,某些小區(qū)域構(gòu)成Eden寥闪,某些構(gòu)成Survivor,某些構(gòu)成老年代磨淌,這些小區(qū)域物理上是不相連的疲憋,并且構(gòu)成新生代和老年代的區(qū)域可以動(dòng)態(tài)改變。
可以通過命令行參數(shù)
-XX:NewRatio=n
來(lái)配置老年代和新生代的比例伦糯,默認(rèn)為2柜某,即比例為2:1;-XX:SurvivorRatio=n
則可以配置Eden與Survivor的比例敛纲,默認(rèn)為8喂击。
Young GC
當(dāng)Eden區(qū)的空間占滿之后,會(huì)觸發(fā)Young GC淤翔,G1將Eden和Survivor中存活的對(duì)象拷貝到Survivor翰绊,或者直接晉升到Old Region中。Young GC的執(zhí)行是多線程并發(fā)的旁壮,期間會(huì)停頓所有的用戶線程(STW)监嗜。
Old GC
當(dāng)堆空間的占用率達(dá)到一定閾值后會(huì)觸發(fā)Old GC(閾值由命令參數(shù)-XX:InitiatingHeapOccupancyPercent
設(shè)定,默認(rèn)值45)抡谐,Old GC分五個(gè)步驟完成:
初始標(biāo)記(Initial Mark裁奇,STW)
對(duì)Survivor區(qū)域掃描,標(biāo)記出可能擁有老年代對(duì)象引用的根區(qū)域(Root Region)麦撵,該階段附屬于一次Young GC的最后階段刽肠,所以是STW。在GC日志中會(huì)被記錄為GC pause (young)(inital-mark)
根區(qū)域掃描(Root Region Scan)
從上一階段標(biāo)記的根區(qū)域中免胃,標(biāo)記所有擁有老年代對(duì)象引用的存活對(duì)象音五,這是一個(gè)并發(fā)的過程,而且必須在進(jìn)行下一次Young GC之前完成
并發(fā)標(biāo)記(Concurrent Marking)
從上一階段標(biāo)記的存活對(duì)象開始羔沙,并發(fā)地跟蹤所有可達(dá)的老年代對(duì)象躺涝,期間可以被Young GC打斷
最終標(biāo)記(Remark,STW)
G1并行地跟蹤在上面階段經(jīng)過更新的存活對(duì)象扼雏,找到未被標(biāo)記的存活的對(duì)象坚嗜,這是一個(gè)STW的階段夯膀,會(huì)用到一個(gè)初始快照(SATB)算法。在此期間完全空閑的區(qū)域會(huì)被直接重置回收苍蔬。
復(fù)制/清除(Copying/Cleanup棍郎,STW)
- G1統(tǒng)計(jì)存活對(duì)象和完全空閑的區(qū)域,完全空閑區(qū)域?qū)⒈恢刂没厥?/li>
- 執(zhí)行清理RSet的操作
- 將存活對(duì)象復(fù)制到新的未被占用的區(qū)域银室。執(zhí)行這一步時(shí)可以只對(duì)新生代操作涂佃,這時(shí)G1將其記錄為
[GC pause (young)]
;也可對(duì)新生代和一部分老年代都進(jìn)行處理蜈敢,這時(shí)被記錄為[GC Pause (mixed)]
辜荠,這些老年代要根據(jù)其“存活度”去選擇。
G1相關(guān)的命令
java命令行參數(shù)
Option | Description |
---|---|
-XX:+UseG1GC | Use the Garbage First (G1) Collector |
-XX:MaxGCPauseMillis=n | Sets a target for the maximum GC pause time. This is a soft goal, and the JVM will make its best effort to achieve it. |
-XX:InitiatingHeapOccupancyPercent=n | Percentage of the (entire) heap occupancy to start a concurrent GC cycle. It is used by GCs that trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations (e.g., G1). A value of 0 denotes 'do constant GC cycles'. The default value is 45. |
-XX:NewRatio=n | Ratio of old/newgeneration sizes. The default value is 2. |
-XX:SurvivorRatio=n | Ratio of eden/survivor space size. The default value is 8. |
-XX:MaxTenuringThreshold=n | Maximum value for tenuring threshold. The default value is 15. |
-XX:ParallelGCThreads=n | Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running. |
-XX:ConcGCThreads=n | Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running. |
-XX:G1ReservePercent=n | Sets the amount of heap that is reserved as a false ceiling to reduce the possibility of promotion failure. The default value is 10. |
-XX:G1HeapRegionSize=n | With G1 the Java heap is subdivided into uniformly sized regions. This sets the size of the individual sub-divisions. The default value of this parameter is determined ergonomically based upon heap size. The minimum value is 1Mb and the maximum value is 32Mb. |
查看JVM默認(rèn)的垃圾回收器
java -XX:+PrintCommandLineFlags -version
如
-XX:G1ConcRefinementThreads=4 -XX:InitialHeapSize=134217728 -XX:MaxHeapSize=2147483648 -XX:+PrintCommandLineFlags -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
查看JVM當(dāng)前堆情況
jmap -heap pid
如
jmap -heap 20932
Attaching to process ID 20932, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
using thread-local object allocation.
Garbage-First (G1) GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 4164943872 (3972.0MB)
NewSize = 1363144 (1.2999954223632812MB)
MaxNewSize = 2498756608 (2383.0MB)
OldSize = 5452592 (5.1999969482421875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 1048576 (1.0MB)
Heap Usage:
G1 Heap:
regions = 3972
capacity = 4164943872 (3972.0MB)
used = 2132264664 (2033.4860458374023MB)
free = 2032679208 (1938.5139541625977MB)
51.19551978442604% used
G1 Young Generation:
Eden Space:
regions = 829
capacity = 2317352960 (2210.0MB)
used = 869269504 (829.0MB)
free = 1448083456 (1381.0MB)
37.51131221719457% used
Survivor Space:
regions = 9
capacity = 9437184 (9.0MB)
used = 9437184 (9.0MB)
free = 0 (0.0MB)
100.0% used
G1 Old Generation:
regions = 1254
capacity = 1741684736 (1661.0MB)
used = 1253557976 (1195.4860458374023MB)
free = 488126760 (465.51395416259766MB)
71.9738739215775% used
19747 interned Strings occupying 1885184 bytes.