write barriers in g1 gc - jfokus...the garbage first collector - pause histogram 32 pause time in...
TRANSCRIPT
Write Barriers in Garbage First Garbage Collector
-By Monica Beckwith Code Karam LLC
@mon_beck; [email protected]
1
©2017 CodeKaram
About me
• Java performance engineer
• I am working as a consultant and an instructor
• I have worked with Oracle, Sun, AMD …
• I used to work in the capacity of G1 GC performance lead @Oracle.
2
©2017 CodeKaram
Agenda• Heap regions and additional data structures
• RSets
• Barriers
• Concurrent refinement
• G1 GC Stages
• Concurrent marking in G1 GC
• Pre-write barrier
• SATB algorithm
3
Regionalized Heap
4
©2017 CodeKaram
Traditional Java Heap
5
Contiguous Java Heap
Eden S0 S1
Old Generation
©2017 CodeKaram
Garbage First GC - Heap Regions
6
Contiguous Java Heap
Free Region
Non-Free Region
©2017 CodeKaram
G1 GC Heap Regions
• Young Regions - Regions that house objects in the Eden and Survivor Spaces
• Old Regions - Regions that house objects in the Old generation.
• Humongous Regions - Regions that house Humongous Objects.
7
Additional Data Structures
8
©2017 CodeKaram
G1 GC Collection Set & Remembered Sets
• Additional data structure to help with maintenance and collection
• Add a slight footprint overhead (~5%)
9
Remembered Sets (RSets)
10
©2017 CodeKaram
• Maintains and tracks incoming references into its region
• old-to-young references
• old-to-old references
11
Remembered Sets
©2017 CodeKaram
• Remembered sets have varying granularity based on the “popularity” of objects or regions.
12
Remembered Sets
©2017 CodeKaram
Remembered Sets
• Different granularities:
• sparse per-region-table (PRT)
• fine-grained PRT
• coarse-grained bitmap
13
©2017 CodeKaram14
Figure 2.3 Remembered sets with incoming object references
RSet Maintenance: Barriers + Refinement
Threads
15
Write Barrier
16
©2017 CodeKaram
Post-Write Barrier
Consider the following assignment:
object.field = some_other_object
G1 GC will issue a write barrier after the reference is updated, hence the name.
17
©2017 CodeKaram
Post-Write BarrierG1 GC filters the need for a barrier by way of a simple check as explained below:
(&object.field XOR &some_other_object) >> RegionSize
If the check evaluates to zero, a barrier is not needed.
If the check != zero, G1 GC enqueues the card in the update log buffer
18
Concurrent Refinement Threads
19
©2017 CodeKaram
Concurrent Refinement Threads
• The refinement threads will scan cards in the filled update log buffers to update the RSets for their corresponding regions.
• The refinement threads are always active
• G1 GC deploys them in a tiered manner to keep up with the filled buffers
20
©2017 CodeKaram
Concurrent Refinement Threads
• Mutator threads can be enlisted to help with processing of filled buffers.
• Avoid this scenario, since the Java application will be halted until the filled buffers are processed!
21
G1 GC Stages
22
A Young Collection
23
©2017 CodeKaram
G1 GC Stages - Young Collection
24
• When young regions are full and no further allocations can happen
• Need to start a stop-the-world collection
• Age objects in survivor regions
• Promote aged objects into the old regions
Marking Threshold
25
©2017 CodeKaram
Initiating Heap Occupancy Percent
26
• Threshold to start a marking cycle to identify candidate old regions for collection during a mixed/incremental collection
• When old generation occupancy crosses this adaptive threshold, a marking cycle can start
Marking
27
©2017 CodeKaram
Stages of Marking
28
• Initial-mark
• Root region scan
• Concurrent mark
• Remark/ Final mark
• Cleanup
A Mixed Collection
29
©2017 CodeKaram
G1 GC Stages - Mixed Collection
30
• When candidate old regions are available and the potential of recovered space is over the (internal) reclaimable threshold
• This is a stop-the-world collection
• All regions in the young generation are included in this collection
• Candidate old regions are added based on the reclaimable space and other min/max thresholds
• Can have multiple mixed collection pauses based on the total old regions identified and a (internal) count target.
G1 GC Pause Histogram
31
©2017 CodeKaram
The Garbage First Collector - Pause Histogram
32
Paus
e tim
e in
milli
seco
nds
0
30
60
90
120
Timestamps3415 3416.3 3417.2 3418.4 3419 3422 3423.4 3432.2 3433.2 3436.8 3437.6 3438.9 3440
Young Collection Initial Mark Remark Cleanup Mixed Collection
Initiating Heap Occupancy Percent
Concurrent Marking in G1 GC
33
©2017 CodeKaram
Recap: Stages of Marking
34
• Initial-mark
• Root region scan
• Concurrent mark
• Remark/ Final mark
• Cleanup
©2017 CodeKaram
Marking Highlights
• Employs ‘Snapshot At The Beginning (SATB) algorithm
• Incremental and concurrent marking algorithm.
• A pre-write barrier is needed to gather the snapshot
35
SATB Algorithm For Concurrent & Incremental
Marking
36
©2017 CodeKaram37
A Java Heap
End
Bottom
~ ~ ~ ~
©2017 CodeKaram38
A Java Heap With Previous Bitmap
End
Bottom
Previous Bitmap
~ ~ ~ ~
©2017 CodeKaram39
A Java Heap (Showing PTAMS) With Previous Bitmap
End
Bottom
Previous Bitmap
PTAMS
Top
~ ~ ~ ~
©2017 CodeKaram40
A Java Heap (Showing PTAMS) With Previous Bitmap
End
Bottom
Previous Bitmap
PTAMS
Objects Are Already Marked
Top
~ ~ ~ ~
©2017 CodeKaram41
A Java Heap (Showing PTAMS + NTAMS) With Previous Bitmap
Top
Bottom
Next Bitmap
PTAMS
NTAMS
End~ ~ ~ ~
©2017 CodeKaram42
Same Java Heap During Concurrent Marking
End
Bottom
PTAMS
NTAMSNew Objects Are
Allocated In This SpaceTop ~ ~ ~ ~
Next Bitmap
©2017 CodeKaram43
Bottom
PTAMS
NTAMS
End
Same Java Heap During Concurrent Marking
Top
Objects Will Be Considered
Implicitly Live
~ ~ ~ ~
Next Bitmap
Pre-Write Barrier
44
©2017 CodeKaram
Pre-Write Barrier
• To record the previous value of the reference fields of objects that were reachable at the start of marking + were a part of the snapshot.
• Prevents those objects from being overwritten by the mutator thread
• Mutator thread logs the previous value of the pointer in an SATB buffer
45
©2017 CodeKaram
Pre-Write Barrier - Pseudo Code
if (marking_is_active) {
pre_val:= x.f;
if (pre_val:= NULL) {
satb_enqueue(pre_val);
}
}
46
©2017 CodeKaram47
End
Bottom
PTAMS
NTAMS
All Live Objects Are Marked
Next Bitmap
Top ~ ~ ~ ~
Same Java Heap At The End Of Remark Pause
©2017 CodeKaram48
Same Java Heap At The End Of Remark Pause
End
Bottom
PTAMS
NTAMS
Top
All Objects Are Implicitly Live
~ ~ ~ ~
Next Bitmap
©2017 CodeKaram49
Same Java Heap During Cleanup
End
Bottom
PTAMS
Top ~ ~ ~ ~
Previous Bitmap
©2017 CodeKaram
References and Additional Reading
• D. L. Detlefs, C. H. Flood, S. Heller, and T. Printezis. Garbage-First Garbage Collection.
• C. Hunt, M. Beckwith, P. Parhar, B. Rutisson. Java Performance Companion.
50