JVM之G1收集器


 

Garbage-First,面向服務端的垃圾收集器。

  • 並行與並發:充分利用多核環境減少停頓時間,
  • 分代收集:不需要配合其它收集器
  • 空間整合:整體上看屬於標記整理算法,局部(region之間)數據復制算法,運作期間不會產生空間碎片
  • 停頓可預測,建立可以預測的停頓時間模型。

內存管理:

  • 將整個java堆划分為多個大小形同的區域region,新生代和老年代都是region的集合。可以有計划的避免在全區域內進行垃圾收集。
  • 回收方式:跟蹤每一個region里面的垃圾堆積的價值大小(回收所得的空間大小以及所需耗費時間的經驗值),維護一個優先列表,每次根據允許的回收時間,優先回收價值最大的region(GI名字由來),
  • region之間的引用,新生代和老年帶之間的引用根據remebered set來避免全盤掃描,每一個region都維護一個remebered set,
  • 初始標記-》並發標記-》最終標記-》篩選回收,類CMS

 

Gabage First: G1 
 
allocation failture,當收集垃圾,從一個區域復制數據到另一個區域時,找不到可用區域時,會引發allocation failture,進而引起full gc (stop the world)
 
float garbage:snapshot-at-the-begining(SATB) 標記存活對象(並發標記開始之前處於存活狀態的對象)
 
 
G1: gc暫停時間及吞吐量的
 
CMS替代者,
 
壓縮(高效)整理收集器,
 
使用等值大小的region作為基本管理單元,極大減少浮動垃圾
 
垃圾回收時間的可預測性更高。允許自定義
 
傳統:
G1:
等大小的region被賦予不同的角色功能。增加了靈活性。
 
==》全局並發標記
 
==》首先收集垃圾比較少的region,釋放大量空間
 
==》在垃圾比較多的的region,執行垃圾收集,壓縮。
 
G1使用可預測暫停模型,來滿足用戶設置的目標。
 
G1一邊壓縮收集垃圾,一邊釋放空間,多線程並行執行(stop the world),減少暫停時間。
 
區別於CMS的不壓縮,ParallelOld的全局壓縮,
 
G1不是實時垃圾回收器,盡可能的實現定義的目標。G1根據以往的收集經驗,對在用戶設定的暫停時間內能夠收集多少region做出估計。模型相對准確,據此判斷哪些,多少region需要被收集。
 
並發標記 + 並行收集
 
Full GC是單線程的,會stop the world
 
Remember Set(RSet):每一個region一個RSet,標記region中的對象引用,空間占用不超過5%.
 
Collection Set(CSet):需要被收集的region集合,CSet中的活躍數據將會被evalucated(copied/moved),可能包含eden,surrior,old,占用空間不超過1%
 
 
應用:
 
大堆(6左右,>=)運行,低GC延遲(<=0.5s)要求,
 
GC頻繁;收集或者壓縮時間長;收集對象年齡差異大。
 
 
區別:
 
CMS:老年代收集;通過並發收集減少暫停時間,通常來說,並發低延遲的收集器都不移動和壓縮活躍數據;通過擴大堆大小來處理碎片化問題。
 
初始標記:對於青年代可達的老年代對象標記;暫停
 
並發標記:貫穿整個老年代,標記活躍對象。基於GC Roots
 
重新標記:檢查並發標記階段漏掉的對象。暫停
 
並發清理:清理收集非活躍對象(合並)
 
重置:清理數據結構,以備下次收集。
 
1:CMS管理下的堆結構
2:年輕代 eden survivor1 survivor2,老年代的對象分配位置后,不再移動。
3:對象從eden,一個survivor復制到另一個survivor,達到收集年齡閾值的,晉升到老年代。
4:yong gc
5:老年代CMS收集:初始標記,重新標記 stop the world
6:CMS並發清理
 
G1:  XX:G1HeapRegionSize
 
年輕代G1收集:
 
1: G1結構:G1將heap划分為多個(2000左右)大小相等的region進行管理。region大小(1~32M)由jvm啟動時決定
2:不同region被映射為邏輯上的不同功能區域,不同區域region不需要連續。活躍數據在不同region復制和移動,region並行收集
3:年輕代收集, STW,eden survivor大小根據存儲的統計信息進行計算設置(為下一次收集使用),
heap划分為多個region管理;yong區域,不連續,易於改變大小;引發暫停;多線程並行收集;復制或升級。
 
老年代G1收集:
Phase
Description
(1) Initial Mark  (Stop the World Event)
This is a stop the world event. With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation.
(2) Root Region Scanning
Scan survivor regions for references into the old generation. This happens while the application continues to run. The phase must be completed before a young GC can occur.
(3) Concurrent Marking
Find live objects over the entire heap. This happens while the application is running. This phase can be interrupted by young generation garbage collections.
(4) Remark (Stop the World Event)
Completes the marking of live object in the heap. Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than what was used in the CMS collector.
(5) Cleanup (Stop the World Event and Concurrent)
  • Performs accounting on live objects and completely free regions. (Stop the world)
  • Scrubs the Remembered Sets. (Stop the world)
  • Reset the empty regions and return them to the free list. (Concurrent)
(*) Copying (Stop the World Event)
These are the stop the world pauses to evacuate or copy live objects to new unused regions. This can be done with young generation regions which are logged as [GC pause (young)]. Or both young and old generation regions which are logged as [GC Pause (mixed)].
 
1:初始標記,STW。基於yong GC,標記survivor中可能引用老年代對象的對象,作為Root Region,並掃描
 
2:並發標記:貫穿整個堆內存,標記活躍對象,並立即清除,同時收集活躍對象統計信息。
3:重新標記:使用snapshot-at-the-beginning(SATB),移除,回收標記的空region。STW
   
4:清理/復制,G1選擇最不活躍的region,以便最快收集。這些區域可以和yong GC同時收集,STW
 
清理:統計活躍對象,活躍區域(STW)=》清理RSet(STW)=》重置空的region=》歸還到free list(並發)。
 
復制:移動活躍對象到未應用的區域(STW)
 
 
yong gc和old gc同時發生。
 
-XX:+UseG1GC
 
java -Xmx50m -Xms50m -XX:+UseG1GC -XX:MaxGCPauseMillis=200
 
-XX:MaxGCPauseMillis=200:
 
-XX:InitiatingHeapOccupancyPercent=45:觸發GC heap使用百分比。
 
最佳實踐:
 
不要設置年輕代大小 --Xmn;G1自動調整
 
XX:MaxGCPauseMillis:設置為能夠滿足90%請求的時間值。
 
Evacuation Failure:沒有足夠的空間。
 
解決:
 
增大-XX:G1ReservePercent; 預留座位假的堆上限百分比,默認10
 
增大-XX:ConcGCThreads 並發收集線程數,不同jvm,默認值不同
 
提前mark
Option and Default Value
Description
-XX:+UseG1GC
Use the Garbage First (G1) Collector:啟用G1收集器
-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.:目標GC暫停時間,盡可能目標
-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.:觸發GC 堆使用比例,整個堆的占用比例,而不是某個分代區域,0意味着頻繁收集,默認為45
-XX:NewRatio=n
Ratio of new/old generation sizes. The default value is 2.:年輕代/老年代比例 默認為2
-XX:SurvivorRatio=n
Ratio of eden/survivor space size. The default value is 8.:eden/survivor比例,默認為8,即 8 1 1
-XX:MaxTenuringThreshold=n
Maximum value for tenuring threshold. The default value is 15.:對象晉升老年代年齡閾值,默認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.:預留預防提升失敗的堆大小上限百分比,默認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.:G1分割堆為等大小的region,region大小默認由jvm根據效能設置,1~32M


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM