G1GC 介紹
2016/12/24
kojilin
關於我
•koji lin
•TWJUG member
•LINE Fukuoka
•Software Engineer
•現在主要都在寫後端
G1GC
•Java SE 7 開始
•Java SE 9 成為預設的 GC (JEP 248)
•減少 pause time
•但可能伴隨較低的 throughput
•比起 CMS,比較不需要去調整參數比較結果
•CMS 也被認為較不適合非常大 heap 的情況
•heap 用 region 分割
•region 的大小會根據 Xmx 變動
G1GC
•region 有分 young gen 和 old gen
O
O
O
E
E
E S
S
•Young GC 發生時
•包含所有的 young region
O
O
O
E
E
E S
S
•Young GC 發生時
•依照狀況變成 Survivor 或 Old
O
O
O
S
S
S
O
51.078: [GC pause (G1 Evacuation Pause) (young)
51.078: [G1Ergonomics (CSet Construction) start choosing CSet, _pending_cards:
0, predicted base time: 29.00 ms, remaining time: 171.00 ms, target pause time:
200.00 ms]
51.078: [G1Ergonomics (CSet Construction) add young regions to CSet, eden:
1079 regions, survivors: 42 regions, predicted young region time: 170.99 ms]
51.078: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 1079
regions, survivors: 42 regions, old: 0 regions, predicted pause time: 199.99 ms,
target pause time: 200.00 ms]
, 0.1092812 secs]
…
[Eden: 2158.0M(2158.0M)->0.0B(1796.0M) Survivors: 84.0M->102.0M
Heap: 2242.0M(4096.0M)->102.0M(4096.0M)]
[Times: user=0.35 sys=0.03, real=0.11 secs]
Collection Set
•Young GC
•包含所有 young 世代的 region
old gen > InitiatingHeapOccupancyPercent
InitiatingHeapOccupancyPercent
•決定 concurrent marking 開始時機
•找出適合回收的 old gen region
•-XX:InitiatingHeapOccupancyPercent=45
•超過總 heap 45% 時開始
24963.770: [G1Ergonomics (Concurrent Cycles) request
concurrent cycle initiation, reason: occupancy higher than
threshold, occupancy: 3013607424 bytes, allocation
request: 0 bytes, threshold: 3006477040 bytes (70.00 %),
source: end of GC]
Concurrent Marking
•Initial marking(STW)
•Root region scanning
•Concurrent marking
•Final mark(STW)
•Clean up(part STW)
Initial Marking
•STW
•用來 marking heap 內所有直接被 root objects 直
接參照到的物件
•跟著 young gc 一起做
Root region scanning
•Concurrently
•將前一步 young gc 後的 survivor regions 參照到
的物件 mark 起來
•所以被稱作 root regions
•必須在下一次 gc 前完成,不然又會出現新的
survivor region
Concurrent Marking
•Concurrently
•Live data accounting
•可以透過 ConcGCThreads 調整效率
Remark
•STW
•完成 live data accounting
•可以透過 ParallelGCThreads 調整效率
Cleanup
•STW
•找出有沒有完全都是垃圾的 region
•依照垃圾清除效率排序 old regions
•Concurrent
•清空沒有 live data 的 region
Concurrent Marking
2016-11-30T23:12:17.318+0900: 24975.061: [GC pause (G1 Evacuation
Pause) (young) (initial-mark)
…
24975.097: [GC concurrent-root-region-scan-start]
24975.115: [GC concurrent-root-region-scan-end, 0.0183740 secs]
24975.115: [GC concurrent-mark-start]
24975.544: [GC concurrent-mark-end, 0.4295200 secs]
24975.545: [GC remark 2016-11-30T23:12:17.803+0900: 24975.545:
[Finalize Marking, 0.0004352 secs]
24975.586: [GC cleanup 2909M->2763M(4096M), 0.0184095 secs]
24975.604: [GC concurrent-cleanup-start]
24975.604: [GC concurrent-cleanup-end, 0.0002595 secs]
•Mixed GC 發生時
•All young gen + some old
O
O
O
S
S
S
O
E E
E
•Mixed GC 發生時
•All young gen + some old
O
O
O
S
S
S
O
E E
E
•Mixed GC 發生時
•All young gen + some old
O
S
O
S
O
SO
24988.167: [G1Ergonomics (CSet Construction) start choosing CSet, _pending_cards:
11490, predicted base time: 11.34 ms, remaining time: 188.66 ms, target pause time:
200.00 ms]
24988.167: [G1Ergonomics (CSet Construction) add young regions to CSet, eden: 92
regions, survivors: 10 regions, predicted young region time: 17.58 ms]
24988.167: [G1Ergonomics (CSet Construction) finish adding old regions to CSet,
reason: predicted time is too high, predicted time: 1.28 ms, remaining time: 0.00 ms, old:
169 regions, min: 169 regions]
24988.167: [G1Ergonomics (CSet Construction) added expensive regions to CSet,
reason: old CSet region num not reached min, old: 169 regions, expensive: 26 regions,
min: 169 regions, remaining time: 0.00 ms]
24988.167: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 92 regions,
survivors: 10 regions, old: 169 regions, predicted pause time: 232.41 ms, target pause
time: 200.00 ms]
24988.205: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old
regions available, candidate old regions: 1179 regions, reclaimable: 2358748744 bytes
(54.92 %), threshold: 4.00 %]
Collection Set
•Young GC
•包含所有 young 世代的 region
•Mixed GC
•包含所有 young 世代的 region 加上一部份垃圾
比例最多的 old 世代的 region
25000.324: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old
regions available, candidate old regions: 185 regions, reclaimable: 311893632 bytes
(7.26 %), threshold: 4.00 %]
…
25006.772: [G1Ergonomics (Mixed GCs) do not continue mixed GCs, reason:
reclaimable percentage not over threshold, candidate old regions: 113 regions,
reclaimable: 170151528 bytes (3.96 %), threshold: 4.00 %]
Parameters for mixed gc
•InitiatingHeapOccupancyPercent = 45
•G1HeapWastePercent = 5
•可以浪費的比例,mixed gc 會決定如果垃圾沒超過這個比例就不做
•主要是為了讓 mixed collection cycle 可以快一點結束
•G1MixedGCCountTarget = 8
•決定一次 concurrent mark 後最多跑幾次 mixed gc
•所有對象 old regions / 8 = 每次 mixed gc CSet 最小數量
•G1MixedGCLiveThresholdPercent = 85
•尋找預計回收 region 時,live data 低於此值才會被當作對象
Humongous Object
•Object < 50% of region size
•會放在 young region
•Object >= 50% of region size
•直接放在 old region,並且 region 內剩餘的空
間不會再被使用
[G1Ergonomics (Concurrent Cycles) request concurrent cycle initiation, reason:
requested by GC cause, GC cause: G1 Humongous Allocation]
•加大 region size
•但要注意因為加大而造成 region 數減少的效能
影響
•找出 root cause
•代表有物件佔著連續的記憶體
•e.g. 很大的陣列,超多欄位的類別
Evacuation Failure
•Survivor 或 old 不夠時
•to-space exhausted
•出現數次後接著就會出現 Full GC
•如果是 survivor
•增加 G1ReservePercent
•-Xms16g -Xmx16g -XX:+UseG1GC
•InitiatingHeapOccupancyPercent: 45
•G1HeapWastePercent: 5
•G1MixedGCLiveThresholdPercent: 85
•G1HeapRegionSize: 8MB
•-Xms16g -Xmx16g -XX:+UseG1GC
•InitiatingHeapOccupancyPercent: 45
•G1HeapWastePercent: 5
•G1MixedGCLiveThresholdPercent: 85
•G1HeapRegionSize: 8MB
16GB x 0.45 = 7.2GB
•-Xms16g -Xmx16g -XX:+UseG1GC
•InitiatingHeapOccupancyPercent: 45
•G1HeapWastePercent: 5
•G1MixedGCLiveThresholdPercent: 85
•G1HeapRegionSize: 8MB
Full GC
•因為 old gen 擠壓到 young gen 的空間,young
gen collection 惡化
•為什麼 Full GC 可以清到只剩 < 500MB ? 但是
mixed GC 無法?
•加上 log 觀察
-XX:+PrintAdaptiveSizePolicy -XX:+PrintGC
-XX:+PrintGCDetails
32631.357: [G1Ergonomics (Concurrent Cycles) request concurrent cycle initiation, reason: occupancy higher than threshold, occupancy:
7725907968 bytes, allocation request: 10047216 bytes, threshold: 7730941095 bytes (45.00 %), source: concurrent humongous allocation]
....
32654.979: [G1Ergonomics (Mixed GCs) start mixed GCs, reason: candidate old regions available, candidate old regions: 457 regions, reclaimable:
2956666176 bytes (17.21 %), threshold: 5.00 %], 0.1106810 secs]
....
[Eden: 6680.0M(6680.0M)->0.0B(536.0M) Survivors: 344.0M->280.0M Heap: 14.0G(16.0G)->7606.6M(16.0G)]
[Times: user=2.31 sys=0.01, real=0.11 secs]
[GC pause (G1 Evacuation Pause) (mixed)
32656.876: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: old CSet region num reached max, old: 205 regions,
max: 205 regions]
32656.876: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 67 regions, survivors: 35 regions, old: 205 regions, predicted pause
time: 173.84 ms, target pause time: 200.00 ms]
32656.992: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old regions available, candidate old regions: 252 regions,
reclaimable: 1321193600 bytes (7.69 %), threshold: 5.00 %]
[Eden: 536.0M(536.0M)->0.0B(720.0M) Survivors: 280.0M->96.0M Heap: 8142.6M(16.0G)->6029.9M(16.0G)]
[Times: user=2.49 sys=0.01, real=0.12 secs]
[GC pause (G1 Evacuation Pause) (mixed)
...
32659.727: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: reclaimable percentage not over threshold, old: 66
regions, max: 205 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %]
32659.727: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 90 regions, survivors: 12 regions, old: 66 regions, predicted pause
time: 120.51 ms, target pause time: 200.00 ms]
...
32659.727: [G1Ergonomics (CSet Construction) finish adding old regions to
CSet, reason: reclaimable percentage not over threshold, old: 66 regions,
max: 205 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %]
32659.727: [G1Ergonomics (CSet Construction) finish choosing CSet, eden:
90 regions, survivors: 12 regions, old: 66 regions, predicted pause time:
120.51 ms, target pause time: 200.00 ms]
32659.785: [G1Ergonomics (Mixed GCs) do not continue mixed GCs,
reason: reclaimable percentage not over threshold, candidate old regions:
186 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %]
[Eden: 720.0M(720.0M)->0.0B(9064.0M) Survivors: 96.0M->64.0M Heap:
6749.9M(16.0G)->5572.0M(16.0G)]
[Times: user=1.20 sys=0.00, real=0.06 secs]
•發現 humongous allocation,尤其在逼近 old gen
使用到近滿時會頻繁觸發 mixed GC.
•找到沒設計好的 http rest api,會取得完整後先
轉換成 byte[] -> json.
•底層使用 okio 的 Buffer,會將大的 byte[] 轉換
成 byte[] 的 linked list.
•但仍不是 root cause.
•調整看看 VM 參數
•InitiatingHeapOccupancyPercent = 30
•G1HeapWastePercent = 3
•G1MixedGCLiveThresholdPercent = 90
•SoftRefLRUPolicyMSPerMB = 50
•不太有用而且因為提早做而降低了 throughput
•超過 30 % 後總是觸發 concurrent marking
•最後沒招了,heap dump 看看
•最後沒招了,heap dump 看看
G1GC
G1GC
•在我們的環境下,問題出在 lettuce 使用的
LatencyUtils 的 LatencyStats &
PauseTracker(Weakreference)
•lettuce 使用 LatencyUtils 來做 command tracking
,並且每 10 分鐘會清掉過去的 LatencyStats 的
reference
*https://github.com/mp911de/lettuce/wiki/Command-Latency-Metrics
G1GC
該不該用 G1GC ?
•照理上沒問題,但 ElasticSearch & Lucene 尚未
建議切換
•G1GC 有些 bug 會造成 index 毀損
•套用後還是要記得觀察結果
•避免過度的調整參數
•盡量使用 MaxGCPausMillis,Xmx,Xms
Java 9
•預設就會是 G1GC
•以前的 JVM log 被廢除,參數跟格式都改變
•-Xlog:[<selection>]:[<output>]:[<decorators>]
e.g. -Xlog:gc*:file=gc.log

More Related Content

PPT
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
PPTX
G1 Garbage Collector - Big Heaps and Low Pauses?
PPTX
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
PDF
-XX:+UseG1GC
PPTX
Hadoop world g1_gc_forh_base_v4
PPTX
G1 collector and tuning and Cassandra
PPTX
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
PDF
Tuning Java for Big Data
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
G1 Garbage Collector - Big Heaps and Low Pauses?
Garbage First Garbage Collector (G1 GC) - Migration to, Expectations and Adva...
-XX:+UseG1GC
Hadoop world g1_gc_forh_base_v4
G1 collector and tuning and Cassandra
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Tuning Java for Big Data

What's hot (20)

PDF
Moving to g1 gc by Kirk Pepperdine.
PDF
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
PDF
The Performance Engineer's Guide to Java (HotSpot) Virtual Machine
PDF
The Performance Engineer's Guide To (OpenJDK) HotSpot Garbage Collection - Th...
PPTX
Tuning Java GC to resolve performance issues
PDF
GC Tuning Confessions Of A Performance Engineer
PDF
Basics of JVM Tuning
PDF
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
PDF
JVM and Garbage Collection Tuning
PDF
Moving to G1GC
PDF
Garbage First Garbage Collector: Where the Rubber Meets the Road!
PDF
Game of Performance: A Song of JIT and GC
PDF
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
PDF
淺談 Java GC 原理、調教和 新發展
PDF
The Performance Engineer's Guide To HotSpot Just-in-Time Compilation
PPTX
Jvm & Garbage collection tuning for low latencies application
PPTX
HBaseCon 2013: A Developer’s Guide to Coprocessors
PPTX
Jvm tuning for low latency application & Cassandra
PDF
Tales from Taming the Long Tail
PDF
Way Improved :) GC Tuning Confessions - presented at JavaOne2015
Moving to g1 gc by Kirk Pepperdine.
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
The Performance Engineer's Guide to Java (HotSpot) Virtual Machine
The Performance Engineer's Guide To (OpenJDK) HotSpot Garbage Collection - Th...
Tuning Java GC to resolve performance issues
GC Tuning Confessions Of A Performance Engineer
Basics of JVM Tuning
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
JVM and Garbage Collection Tuning
Moving to G1GC
Garbage First Garbage Collector: Where the Rubber Meets the Road!
Game of Performance: A Song of JIT and GC
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
淺談 Java GC 原理、調教和 新發展
The Performance Engineer's Guide To HotSpot Just-in-Time Compilation
Jvm & Garbage collection tuning for low latencies application
HBaseCon 2013: A Developer’s Guide to Coprocessors
Jvm tuning for low latency application & Cassandra
Tales from Taming the Long Tail
Way Improved :) GC Tuning Confessions - presented at JavaOne2015
Ad

Similar to G1GC (20)

PPTX
GC Tuning: Fortune 500 Case Studies on Cutting Costs and Boosting Performance
PPTX
Am I reading GC logs Correctly?
PPTX
JVM memory management & Diagnostics
PPTX
Pick diamonds from garbage
PPTX
Become a Garbage Collection Hero
PPTX
Hadoop Meetup Jan 2019 - Dynamometer and a Case Study in NameNode GC
PDF
Java 9: The (G1) GC Awakens!
PPTX
GC Tuning: Fortune 500 Case Studies on Cutting Costs and Boosting Performance
PDF
Low latency Java apps
PPTX
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
PPTX
Become a Java GC Hero - All Day Devops
PDF
ZGC-SnowOne.pdf
PPTX
GC Tuning & Troubleshooting Crash Course
PDF
Tp cms-g1-v4
PPTX
Progress_190130
PPTX
Become a Java GC Hero - ConFoo Conference
PPTX
GC Tuning: A Masterpiece in Performance Engineering
PDF
Low latency & mechanical sympathy issues and solutions
PPTX
Garbage Collection of Java VM
PPT
Performance tuning jvm
GC Tuning: Fortune 500 Case Studies on Cutting Costs and Boosting Performance
Am I reading GC logs Correctly?
JVM memory management & Diagnostics
Pick diamonds from garbage
Become a Garbage Collection Hero
Hadoop Meetup Jan 2019 - Dynamometer and a Case Study in NameNode GC
Java 9: The (G1) GC Awakens!
GC Tuning: Fortune 500 Case Studies on Cutting Costs and Boosting Performance
Low latency Java apps
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
Become a Java GC Hero - All Day Devops
ZGC-SnowOne.pdf
GC Tuning & Troubleshooting Crash Course
Tp cms-g1-v4
Progress_190130
Become a Java GC Hero - ConFoo Conference
GC Tuning: A Masterpiece in Performance Engineering
Low latency & mechanical sympathy issues and solutions
Garbage Collection of Java VM
Performance tuning jvm
Ad

More from koji lin (18)

PDF
サーバーサイドでの非同期処理で色々やったよ
PDF
Using armeria to write your RPC
PDF
使用 Java 上的 future/promise API
PDF
Annotation processing and code gen
PDF
Jcconf
PDF
Use Lambdas in Android
PDF
docker intro
PDF
Java8 time
PDF
Java8 stream
PDF
Java8 lambda
PDF
Idea13
ODP
CompletableFuture
ODP
Raspberry Pi with Java
PDF
Services you can use to monitor and analyze mobile app
PDF
Programming with Threads in Java
PDF
JQuery
PPT
山頂洞人日記 - 回歸到最純樸的開發
ODP
Android Location-based應用開發分享
サーバーサイドでの非同期処理で色々やったよ
Using armeria to write your RPC
使用 Java 上的 future/promise API
Annotation processing and code gen
Jcconf
Use Lambdas in Android
docker intro
Java8 time
Java8 stream
Java8 lambda
Idea13
CompletableFuture
Raspberry Pi with Java
Services you can use to monitor and analyze mobile app
Programming with Threads in Java
JQuery
山頂洞人日記 - 回歸到最純樸的開發
Android Location-based應用開發分享

Recently uploaded (20)

PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
ERP Manufacturing Modules & Consulting Solutions : Contetra Pvt Ltd
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
PDF
Mobile App Backend Development with WordPress REST API: The Complete eBook
PDF
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
PDF
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
PPTX
HackYourBrain__UtrechtJUG__11092025.pptx
PDF
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
PDF
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
PPTX
Chapter 1 - Transaction Processing and Mgt.pptx
PDF
Lumion Pro Crack New latest version Download 2025
PPTX
Lecture 5 Software Requirement Engineering
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PPTX
Human Computer Interaction lecture Chapter 2.pptx
PPT
3.Software Design for software engineering
PPTX
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PDF
IDM Crack 6.42 Build 42 Patch Serial Key 2025 Free New Version
PDF
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
PDF
AI-Powered Fuzz Testing: The Future of QA
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
ERP Manufacturing Modules & Consulting Solutions : Contetra Pvt Ltd
Practical Indispensable Project Management Tips for Delivering Successful Exp...
Mobile App Backend Development with WordPress REST API: The Complete eBook
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
HackYourBrain__UtrechtJUG__11092025.pptx
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
Chapter 1 - Transaction Processing and Mgt.pptx
Lumion Pro Crack New latest version Download 2025
Lecture 5 Software Requirement Engineering
Viber For Windows 25.7.1 Crack + Serial Keygen
Human Computer Interaction lecture Chapter 2.pptx
3.Software Design for software engineering
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
Cloud Native Aachen Meetup - Aug 21, 2025
IDM Crack 6.42 Build 42 Patch Serial Key 2025 Free New Version
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
AI-Powered Fuzz Testing: The Future of QA

G1GC

  • 2. 關於我 •koji lin •TWJUG member •LINE Fukuoka •Software Engineer •現在主要都在寫後端
  • 3. G1GC •Java SE 7 開始 •Java SE 9 成為預設的 GC (JEP 248) •減少 pause time •但可能伴隨較低的 throughput •比起 CMS,比較不需要去調整參數比較結果 •CMS 也被認為較不適合非常大 heap 的情況
  • 4. •heap 用 region 分割 •region 的大小會根據 Xmx 變動
  • 6. •region 有分 young gen 和 old gen O O O E E E S S
  • 7. •Young GC 發生時 •包含所有的 young region O O O E E E S S
  • 8. •Young GC 發生時 •依照狀況變成 Survivor 或 Old O O O S S S O
  • 9. 51.078: [GC pause (G1 Evacuation Pause) (young) 51.078: [G1Ergonomics (CSet Construction) start choosing CSet, _pending_cards: 0, predicted base time: 29.00 ms, remaining time: 171.00 ms, target pause time: 200.00 ms] 51.078: [G1Ergonomics (CSet Construction) add young regions to CSet, eden: 1079 regions, survivors: 42 regions, predicted young region time: 170.99 ms] 51.078: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 1079 regions, survivors: 42 regions, old: 0 regions, predicted pause time: 199.99 ms, target pause time: 200.00 ms] , 0.1092812 secs] … [Eden: 2158.0M(2158.0M)->0.0B(1796.0M) Survivors: 84.0M->102.0M Heap: 2242.0M(4096.0M)->102.0M(4096.0M)] [Times: user=0.35 sys=0.03, real=0.11 secs]
  • 11. old gen > InitiatingHeapOccupancyPercent
  • 12. InitiatingHeapOccupancyPercent •決定 concurrent marking 開始時機 •找出適合回收的 old gen region •-XX:InitiatingHeapOccupancyPercent=45 •超過總 heap 45% 時開始
  • 13. 24963.770: [G1Ergonomics (Concurrent Cycles) request concurrent cycle initiation, reason: occupancy higher than threshold, occupancy: 3013607424 bytes, allocation request: 0 bytes, threshold: 3006477040 bytes (70.00 %), source: end of GC]
  • 14. Concurrent Marking •Initial marking(STW) •Root region scanning •Concurrent marking •Final mark(STW) •Clean up(part STW)
  • 15. Initial Marking •STW •用來 marking heap 內所有直接被 root objects 直 接參照到的物件 •跟著 young gc 一起做
  • 16. Root region scanning •Concurrently •將前一步 young gc 後的 survivor regions 參照到 的物件 mark 起來 •所以被稱作 root regions •必須在下一次 gc 前完成,不然又會出現新的 survivor region
  • 17. Concurrent Marking •Concurrently •Live data accounting •可以透過 ConcGCThreads 調整效率
  • 18. Remark •STW •完成 live data accounting •可以透過 ParallelGCThreads 調整效率
  • 20. Concurrent Marking 2016-11-30T23:12:17.318+0900: 24975.061: [GC pause (G1 Evacuation Pause) (young) (initial-mark) … 24975.097: [GC concurrent-root-region-scan-start] 24975.115: [GC concurrent-root-region-scan-end, 0.0183740 secs] 24975.115: [GC concurrent-mark-start] 24975.544: [GC concurrent-mark-end, 0.4295200 secs] 24975.545: [GC remark 2016-11-30T23:12:17.803+0900: 24975.545: [Finalize Marking, 0.0004352 secs] 24975.586: [GC cleanup 2909M->2763M(4096M), 0.0184095 secs] 24975.604: [GC concurrent-cleanup-start] 24975.604: [GC concurrent-cleanup-end, 0.0002595 secs]
  • 21. •Mixed GC 發生時 •All young gen + some old O O O S S S O E E E
  • 22. •Mixed GC 發生時 •All young gen + some old O O O S S S O E E E
  • 23. •Mixed GC 發生時 •All young gen + some old O S O S O SO
  • 24. 24988.167: [G1Ergonomics (CSet Construction) start choosing CSet, _pending_cards: 11490, predicted base time: 11.34 ms, remaining time: 188.66 ms, target pause time: 200.00 ms] 24988.167: [G1Ergonomics (CSet Construction) add young regions to CSet, eden: 92 regions, survivors: 10 regions, predicted young region time: 17.58 ms] 24988.167: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: predicted time is too high, predicted time: 1.28 ms, remaining time: 0.00 ms, old: 169 regions, min: 169 regions] 24988.167: [G1Ergonomics (CSet Construction) added expensive regions to CSet, reason: old CSet region num not reached min, old: 169 regions, expensive: 26 regions, min: 169 regions, remaining time: 0.00 ms] 24988.167: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 92 regions, survivors: 10 regions, old: 169 regions, predicted pause time: 232.41 ms, target pause time: 200.00 ms] 24988.205: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old regions available, candidate old regions: 1179 regions, reclaimable: 2358748744 bytes (54.92 %), threshold: 4.00 %]
  • 25. Collection Set •Young GC •包含所有 young 世代的 region •Mixed GC •包含所有 young 世代的 region 加上一部份垃圾 比例最多的 old 世代的 region
  • 26. 25000.324: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old regions available, candidate old regions: 185 regions, reclaimable: 311893632 bytes (7.26 %), threshold: 4.00 %] … 25006.772: [G1Ergonomics (Mixed GCs) do not continue mixed GCs, reason: reclaimable percentage not over threshold, candidate old regions: 113 regions, reclaimable: 170151528 bytes (3.96 %), threshold: 4.00 %]
  • 27. Parameters for mixed gc •InitiatingHeapOccupancyPercent = 45 •G1HeapWastePercent = 5 •可以浪費的比例,mixed gc 會決定如果垃圾沒超過這個比例就不做 •主要是為了讓 mixed collection cycle 可以快一點結束 •G1MixedGCCountTarget = 8 •決定一次 concurrent mark 後最多跑幾次 mixed gc •所有對象 old regions / 8 = 每次 mixed gc CSet 最小數量 •G1MixedGCLiveThresholdPercent = 85 •尋找預計回收 region 時,live data 低於此值才會被當作對象
  • 28. Humongous Object •Object < 50% of region size •會放在 young region •Object >= 50% of region size •直接放在 old region,並且 region 內剩餘的空 間不會再被使用
  • 29. [G1Ergonomics (Concurrent Cycles) request concurrent cycle initiation, reason: requested by GC cause, GC cause: G1 Humongous Allocation]
  • 30. •加大 region size •但要注意因為加大而造成 region 數減少的效能 影響 •找出 root cause •代表有物件佔著連續的記憶體 •e.g. 很大的陣列,超多欄位的類別
  • 31. Evacuation Failure •Survivor 或 old 不夠時 •to-space exhausted •出現數次後接著就會出現 Full GC •如果是 survivor •增加 G1ReservePercent
  • 32. •-Xms16g -Xmx16g -XX:+UseG1GC •InitiatingHeapOccupancyPercent: 45 •G1HeapWastePercent: 5 •G1MixedGCLiveThresholdPercent: 85 •G1HeapRegionSize: 8MB
  • 33. •-Xms16g -Xmx16g -XX:+UseG1GC •InitiatingHeapOccupancyPercent: 45 •G1HeapWastePercent: 5 •G1MixedGCLiveThresholdPercent: 85 •G1HeapRegionSize: 8MB 16GB x 0.45 = 7.2GB
  • 34. •-Xms16g -Xmx16g -XX:+UseG1GC •InitiatingHeapOccupancyPercent: 45 •G1HeapWastePercent: 5 •G1MixedGCLiveThresholdPercent: 85 •G1HeapRegionSize: 8MB Full GC
  • 35. •因為 old gen 擠壓到 young gen 的空間,young gen collection 惡化
  • 36. •為什麼 Full GC 可以清到只剩 < 500MB ? 但是 mixed GC 無法? •加上 log 觀察 -XX:+PrintAdaptiveSizePolicy -XX:+PrintGC -XX:+PrintGCDetails
  • 37. 32631.357: [G1Ergonomics (Concurrent Cycles) request concurrent cycle initiation, reason: occupancy higher than threshold, occupancy: 7725907968 bytes, allocation request: 10047216 bytes, threshold: 7730941095 bytes (45.00 %), source: concurrent humongous allocation] .... 32654.979: [G1Ergonomics (Mixed GCs) start mixed GCs, reason: candidate old regions available, candidate old regions: 457 regions, reclaimable: 2956666176 bytes (17.21 %), threshold: 5.00 %], 0.1106810 secs] .... [Eden: 6680.0M(6680.0M)->0.0B(536.0M) Survivors: 344.0M->280.0M Heap: 14.0G(16.0G)->7606.6M(16.0G)] [Times: user=2.31 sys=0.01, real=0.11 secs] [GC pause (G1 Evacuation Pause) (mixed) 32656.876: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: old CSet region num reached max, old: 205 regions, max: 205 regions] 32656.876: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 67 regions, survivors: 35 regions, old: 205 regions, predicted pause time: 173.84 ms, target pause time: 200.00 ms] 32656.992: [G1Ergonomics (Mixed GCs) continue mixed GCs, reason: candidate old regions available, candidate old regions: 252 regions, reclaimable: 1321193600 bytes (7.69 %), threshold: 5.00 %] [Eden: 536.0M(536.0M)->0.0B(720.0M) Survivors: 280.0M->96.0M Heap: 8142.6M(16.0G)->6029.9M(16.0G)] [Times: user=2.49 sys=0.01, real=0.12 secs] [GC pause (G1 Evacuation Pause) (mixed) ... 32659.727: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: reclaimable percentage not over threshold, old: 66 regions, max: 205 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %] 32659.727: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 90 regions, survivors: 12 regions, old: 66 regions, predicted pause time: 120.51 ms, target pause time: 200.00 ms]
  • 38. ... 32659.727: [G1Ergonomics (CSet Construction) finish adding old regions to CSet, reason: reclaimable percentage not over threshold, old: 66 regions, max: 205 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %] 32659.727: [G1Ergonomics (CSet Construction) finish choosing CSet, eden: 90 regions, survivors: 12 regions, old: 66 regions, predicted pause time: 120.51 ms, target pause time: 200.00 ms] 32659.785: [G1Ergonomics (Mixed GCs) do not continue mixed GCs, reason: reclaimable percentage not over threshold, candidate old regions: 186 regions, reclaimable: 857822432 bytes (4.99 %), threshold: 5.00 %] [Eden: 720.0M(720.0M)->0.0B(9064.0M) Survivors: 96.0M->64.0M Heap: 6749.9M(16.0G)->5572.0M(16.0G)] [Times: user=1.20 sys=0.00, real=0.06 secs]
  • 39. •發現 humongous allocation,尤其在逼近 old gen 使用到近滿時會頻繁觸發 mixed GC. •找到沒設計好的 http rest api,會取得完整後先 轉換成 byte[] -> json. •底層使用 okio 的 Buffer,會將大的 byte[] 轉換 成 byte[] 的 linked list. •但仍不是 root cause.
  • 40. •調整看看 VM 參數 •InitiatingHeapOccupancyPercent = 30 •G1HeapWastePercent = 3 •G1MixedGCLiveThresholdPercent = 90 •SoftRefLRUPolicyMSPerMB = 50 •不太有用而且因為提早做而降低了 throughput •超過 30 % 後總是觸發 concurrent marking
  • 45. •在我們的環境下,問題出在 lettuce 使用的 LatencyUtils 的 LatencyStats & PauseTracker(Weakreference) •lettuce 使用 LatencyUtils 來做 command tracking ,並且每 10 分鐘會清掉過去的 LatencyStats 的 reference *https://github.com/mp911de/lettuce/wiki/Command-Latency-Metrics
  • 47. 該不該用 G1GC ? •照理上沒問題,但 ElasticSearch & Lucene 尚未 建議切換 •G1GC 有些 bug 會造成 index 毀損 •套用後還是要記得觀察結果 •避免過度的調整參數 •盡量使用 MaxGCPausMillis,Xmx,Xms
  • 48. Java 9 •預設就會是 G1GC •以前的 JVM log 被廢除,參數跟格式都改變 •-Xlog:[<selection>]:[<output>]:[<decorators>] e.g. -Xlog:gc*:file=gc.log