low pause gc in hotspot
TRANSCRIPT
![Page 2: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/2.jpg)
Outline
Introduction
State of Java GCRecent ChangesFuture
Under The HoodG1ShenandoahG1 vs Shenandoah
Conclusion
![Page 3: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/3.jpg)
Introduction
![Page 4: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/4.jpg)
What are our goals.
I Control pause times
I Control heap size
I Control throughput
I Not OOM
![Page 5: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/5.jpg)
What HotSpot GCs are there.
I Serial
I Parallel
I CMS
I iCMS
I G1
I Shenandoah
![Page 6: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/6.jpg)
State of Java GC
![Page 7: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/7.jpg)
Recent Changes
![Page 8: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/8.jpg)
Permgen to Metaspace.
I Permanent stu� is now held in native space
I Simplify con�guration
I JEP 156: G1 GC: Reduce need for full GCs
I Strings moved to the heap
I Can tune with:
I �XX:MaxMetaspaceSizeI �XX:MetaspaceSize
![Page 9: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/9.jpg)
G1 Ready
I Stabilised
I Producing consistent results
I Not crashing
![Page 10: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/10.jpg)
Future
![Page 11: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/11.jpg)
The future of GC
I JEP 248: Make G1 the Default Garbage Collector
I Fundamentally changes the default behavior from highthroughput to low pause
I JEP 192: String Deduplication in G1
![Page 12: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/12.jpg)
The future of GC
I JEP 189: Shenandoah: An Ultra-Low-Pause-Time GarbageCollector
![Page 13: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/13.jpg)
Remove Old GC combinations
I JEP 173: Retire Some Rarely-Used GC Combinations
I JEP 214: Remove GC Combinations Deprecated in JDK 8
I DefNew + CMSI ParNew + SerialOldI Incremental CMS
![Page 14: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/14.jpg)
Speculative
I JEP draft: Parallelize the Full GC Phase in CMS
I https://bugs.openjdk.java.net/browse/JDK-8130200
I JEP 271: Uni�ed GC Logging
![Page 15: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/15.jpg)
Under The Hood
![Page 16: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/16.jpg)
Other Collectors
Parallel - High throughputCMS - Low pause
![Page 17: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/17.jpg)
G1
![Page 18: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/18.jpg)
The Rise of G1
I Easier to tune (-XX:MaxGCPauseMillis=N)
I Can set pause goals
I Compacting
![Page 19: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/19.jpg)
Heap Layout And GC
Eden
Survivo r 1
Survivo r 2
Tenured
Young Gen
![Page 20: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/20.jpg)
G1 Heap
Eden
Old
Survivor
Empty
![Page 21: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/21.jpg)
G1 How it works
I Mark and evacuate style
I Snapshot at the beginning
I Scan from rootsI Track mutations in the graph
![Page 22: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/22.jpg)
G1 Heap
Eden
Old
Survivor
![Page 23: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/23.jpg)
G1 Heap Evacuation
Eden
Old
Survivor
![Page 24: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/24.jpg)
G1 How it works
I Mark concurrently
I Pause to evacuate
I Don't evacuate all at once
I Divide the evacuation work up and every time we have a YGpause, do a bit of OG work
![Page 25: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/25.jpg)
G1 How it works
CSET
Pass IHOP
YGYG
YG
Mixed
Mixed
Mixed
YG
YG
![Page 26: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/26.jpg)
G1 How it works
Young Collections Mixed Collections
Heap passes Initiating Heap Occupancy Percent
No more eligible regions
![Page 27: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/27.jpg)
G1 How it works
I Start Marking
I -XX:InitiatingHeapOccupancyPercent=n
I Mixed GC until no more eligible regions
I -XX:G1MixedGCLiveThresholdPercent
![Page 28: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/28.jpg)
Tuning Parameters
Xmx/Xms Heap Size
MaxGCPauseMillis Target pause limit
G1MixedGcCountTarget Target number of mixed garbagecollections
G1OldCSetRegionThresholdPercent Limit on the number of oldregions in a cset
G1MixedGCLiveThresholdPercent Threshold for an old region to beincluded in a mixed garbage collection cycle
G1HeapWastePercent Level of �oating garbage you are ok with
Ref: http://www.oracle.com/technetwork/articles/java/g1gc-1984535.html (http://bit.ly/1AC7JDZ)
![Page 29: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/29.jpg)
The problem
I The whole of YG is cleaned during every GC
I YG is a low bound on how low you can get your pauses
I If Ergonomics does not reduce YG su�ciently, sucks to be you
I Only way to �x this is reduce the YG workload
I Best way found to do this is forcibly reduce the size of YG.
![Page 30: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/30.jpg)
The problem
I �Object Copy� and �Ext Root Scanning� tend to dominate
I Object copy: Time spent copying live objects, whenevacuating regions.
I Ext Root Scanning: Time spent scanning external roots(registers, thread stacks, etc)
![Page 31: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/31.jpg)
The problem
I On our log DB (many of these are older logs):
I 40% of all �Parallel Time� is spent in Object CopyI 50% in Ext Root Scanning
I However Object Copy seems to dominate apps that are underGC pressure
I Little can be done to speed up this process.
![Page 32: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/32.jpg)
The problem
![Page 33: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/33.jpg)
The problem
![Page 34: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/34.jpg)
Conclusion
I Easier to tune
I Bound by Object copy
I Have to pause to evacuateI Need concurrent relocation
![Page 35: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/35.jpg)
Shenandoah
![Page 36: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/36.jpg)
Shenandoah
I Compacting
I Concurrently relocates objects
I �Goal is to have < 10ms pause times for 100gb+ heaps.�
I �If you are running with a heap of 20GB or less or if you arerunning with fewer than eight cores, you are probably bettero� with one of the current GC algorithms�
![Page 37: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/37.jpg)
Shenandoah
I Roman Kennke - https://rkennke.wordpress.com/
I Christine H. Flood - https://christine�ood.wordpress.com/
I http://openjdk.java.net/jeps/189
![Page 38: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/38.jpg)
Shenandoah
I Regional heap similar to G1
I Evacuate areas with high garbage (garbage �rst)
I Non-generational
![Page 39: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/39.jpg)
Shenandoah Phases
I Mark
I Evacuate
![Page 40: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/40.jpg)
Concurrent Mark
I Snapshot at the beginning
I Full heap
![Page 41: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/41.jpg)
Concurrent Relocation
I Use Brooks pointers
I All objects have a pointer that normally point to themselvesI When relocated pointer updated to point to new locationI Reading and writing threads follow pointer to the �real� object
![Page 42: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/42.jpg)
Brooks Pointers
{foo:bar}
{foo:bar}
{foo:bar}
From To
From To
![Page 43: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/43.jpg)
Brooks Pointers
{foo:bar}
{foo:bar}
{foo:baz}
From To
From To
![Page 44: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/44.jpg)
Brooks Pointers
I Read(Read(pointer))
I Objects evacuated from �From� spaces, to �To� spaces
I Objects relocated on a write
I Pointer in old object updated via CAS operation
![Page 45: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/45.jpg)
Fixing Up Pointers
I 2 Options
I Fix up pointers in the next mark (default)
I May require a lot of head room in the heapI Pay a cost of indirection on relocated objects between GC'sI Cache miss?
I Accept another pause
I No remembered sets
![Page 46: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/46.jpg)
Write Barrier
I Enforce only write in To space
I Perform relocation if needed
I Avoid read relocation storm
I Ensure previous pointers are still marked
![Page 47: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/47.jpg)
Costs
I Extra heap space for forwarding pointer
I Read/Write barrier
I Pointer chasing may make memory access patternunpredictable
I Shenandoah devs believe overhead can be kept reasonably low
![Page 48: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/48.jpg)
G1 vs Shenandoah
![Page 49: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/49.jpg)
Tests
I Shenandoah
I -XX:+UseShenandoahGC
-XX:-ClassUnloadingWithConcurrentMark
-XX:MaxHeapSize=25G
I G1
I -XX:+UseG1GC -XX:MaxGCPauseMillis=100
-XX:MaxHeapSize=25G
I Environment
I Spring web appI Load test from external machine running GatlingI AWS, 8 core, 32GB ram
![Page 50: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/50.jpg)
G1 vs Shenandoah
G1 Shenandoah
![Page 51: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/51.jpg)
G1 vs Shenandoah - Response Times
G1
Shenandoah
![Page 52: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/52.jpg)
G1 vs Shenandoah- Response Times (No Full GC)
G1
Shenandoah
![Page 53: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/53.jpg)
G1 vs Shenandoah - GC event counts
Time(ms)
Time(ms)
Event C
ountE
vent Count
![Page 54: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/54.jpg)
G1 vs Shenandoah Pause Times, 0-99th Percentile
Percentile
Paus
e(m
s)
![Page 55: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/55.jpg)
G1 vs Shenandoah Pause Times, 0-100th Percentile
Percentile
Paus
e(m
s)
![Page 56: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/56.jpg)
G1 vs Shenandoah Response Times, 0-99th Percentile
Percentile
Paus
e(m
s)
![Page 57: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/57.jpg)
G1 vs Shenandoah Response Times, 0-100th Percentile
Percentile
Paus
e(m
s)
![Page 58: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/58.jpg)
Results
I Still very early days for Shenandoah
I Far younger than G1 and far less engineering timeI Has comparable results to G1
I Beware of single benchmarks and results
I Application stopped time
I STOP THE PRESSES
I Since performing this talk, following advice from RomanKennke I have managed to produce stable results with <60mspauses at the 99th percentile and no full GCs.
![Page 59: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/59.jpg)
Conclusion
![Page 60: Low pause GC in HotSpot](https://reader034.vdocuments.mx/reader034/viewer/2022042907/5871ad821a28abda6a8b5ec3/html5/thumbnails/60.jpg)
Conclusion
I G1
I Production readyI Bound by object copy
I Shenandoah
I Looks very promising but not ready yetI Will remove the bound on object copyI Java 10
I Join Friends of jClarity - [email protected]
I Send in your GC logs