Slide 1

Slide 1 text

©2020 Alex Blewitt Taking out the garbage An overview of GCs in the JVM

Slide 2

Slide 2 text

©2020 Alex Blewitt Generational Garbage Collection From Young to Old • Generational Hypothesis: Most objects die young • Have a young generation which is cleared periodically • Promote long-lived objects to older generations Eden Survivor Old Young

Slide 3

Slide 3 text

©2020 Alex Blewitt Eden • The Eden space is where (most) objects are allocated • Split into different thread sections called TLABs • When a thread allocates, it does so into the TLAB and bumps pointer • When out of space requests a new TLAB from Eden • If no new TLAB is available, triggers a minor collection Eden TLAB TLAB TLAB TLAB TLAB TLAB TLAB

Slide 4

Slide 4 text

©2020 Alex Blewitt Allocations outside TLAB • Some allocations are handled outside of a TLAB • If the TLAB cannot accommodate size but would waste TLAB space • If the allocation is too large for Eden and stored straight in the Old region • Small allocations outside TLAB are infrequent • Can be used to do periodic monitoring of object creation • Waste TLAB space and waste Eden space filled with dummy object • Makes parsing heaps easier

Slide 5

Slide 5 text

©2020 Alex Blewitt Minor GC • A Minor GC collects the objects in the young generation • Eden survivors are swept into the Survivor To space • Survivor From survivors are swept into the Survivor To space • Oldest objects overflow into Old space Eden Old Survivor

Slide 6

Slide 6 text

©2020 Alex Blewitt Minor GC • A Minor GC collects the objects in the young generation • Eden survivors are swept into the Survivor To space • Survivor From survivors are swept into the Survivor To space • Oldest objects overflow into Old space Eden To Old From

Slide 7

Slide 7 text

©2020 Alex Blewitt Object age • Each object has an age – the number of GCs that it has survived • Objects older than this age "Tenuring threshold" are moved into Old gen • Typically objects of a particular class have high or low ages • Objects with all ages may indicate a memory leak

Slide 8

Slide 8 text

©2020 Alex Blewitt Garbage Collection Algorithms • Young and Old gen can have different garbage collection algorithms • Young: Serial, ParallelNew, ParallelScavenge • Old: Serial, ParallelOld, ConcurrentMarkSweep* • May have a combined collector which handles everything • G1, Shenandoah, Z • Some combinations are deprecated and will be removed in future

Slide 9

Slide 9 text

©2020 Alex Blewitt Application Pause Time b c d a Application Threads

Slide 10

Slide 10 text

©2020 Alex Blewitt Application Pause Time a b c d Application Threads Safepoint Trigger Safepoint Start

Slide 11

Slide 11 text

©2020 Alex Blewitt Application Pause Time a b c d a a a a Application Threads Safepoint Trigger Safepoint Start Safepoint End GC Threads GC Threads GC time Application Stopped Time Time To Safepoint

Slide 12

Slide 12 text

©2020 Alex Blewitt Parallel GC a b c d a a a a Application Threads GC Threads GC Threads

Slide 13

Slide 13 text

©2020 Alex Blewitt Serial GC a b c d a Application Threads GC Threads GC Threads

Slide 14

Slide 14 text

©2020 Alex Blewitt Concurrent GC a b c d a Application Threads GC Threads GC Threads a

Slide 15

Slide 15 text

©2020 Alex Blewitt Concurrent Parallel GC a b c d a Application Threads GC Threads GC Threads a a a a

Slide 16

Slide 16 text

©2020 Alex Blewitt Root sets • Live objects begin with the root sets • Thread roots (local variables, stack slots, oops used in methods) • Interned Strings • JNI references • These then point to other references • Class roots (ClassLoader(s), classes and static variables) • Instances

Slide 17

Slide 17 text

©2020 Alex Blewitt Regional concurrent collectors • Regional collectors split the memory into different regions (or pages) • Collection will process a subset of them in one go • Pause times can be adjusted by collecting different numbers of regions • Regions have flavours of 'Eden', 'To' and GCs operate similarly • ZGC uses multiply mapped virtual memory tags to identify mapped/marked objs • Updating references can co-opt application threads to fix on the fly • By moving more work into concurrent/application threads, reduce pause time

Slide 18

Slide 18 text

©2020 Alex Blewitt Garbage First collector • G1 tries to collect as much garbage first as it can • Collects Eden and Survivor regions, and some Old regions as well • Concurrently marks objects • Stops the world for relocations (evacuations) E S O H E S O E S O H O E S O H S O O O H O

Slide 19

Slide 19 text

©2020 Alex Blewitt Shenandoah forwarding pointers • Shenandoah concurrently evacuates objects from the 'From' to 'To' spaces • Needs to store old/new but ensure that users only use new copy • Stores a forwarding pointer during GC evacuation in old object mark word • Load barriers used to correctly identify new object location • Only when GC is operational and object is in collection set • Load barrier can heal stale references in application thread upon load • When all references to old object are updated it can be released • Can run on 64-bit or 32-bit systems

Slide 20

Slide 20 text

©2020 Alex Blewitt Shenandoah algorithm https://wiki.openjdk.java.net/display/shenandoah/Main

Slide 21

Slide 21 text

©2020 Alex Blewitt ZGC coloured/tagged pointers • ZGC has a similar from/to space but uses pointer colours to distinguish • High order bits have from/to encoded in them • Bitmask says if coloured pointer is correct for this phase • Correction taken by application thread to update incorrect pointer • Same physical memory is mapped in to multiple virtual address spaces • Read barrier can be hit by marking virtual memory as non-readable • Requires 64-bit VM memory to operate

Slide 22

Slide 22 text

©2020 Alex Blewitt ZGC memory mapping Mem Mem Mem Mem O O O O O References are repaired on load Memory mapped multiple times Memory split into pages like G1/Shenandoah regions

Slide 23

Slide 23 text

©2020 Alex Blewitt Comparison G1 Shenandoah Z Regional ✅ ✅ ✅ Generational ✅ Young STW ✅ Concurrent Evacuation ✅ ✅ x86_32 ✅ ✅ x86_64 ✅ ✅ ✅ Compressed Oops ✅ ✅ Return Memory 12+ ✅ 13+ Memory 2M-? 8M-? 8M-16T Max Pause ~1s ~1ms ~1ms Supported 8-14 8u,11u,12-14 11-14 8u and 11u Backports Both Shenandoah and ZGC need UnlockExperimentalVMOptions but plan to graduate to non-experimental in JDK 15

Slide 24

Slide 24 text

©2020 Alex Blewitt Summary • Java GCs continue to evolve to provide larger memory and lower pauses • Pauses are now limited by root set (number of threads, size of stack) • Most operations are performed concurrently in regional GCs • G1 evacuation is still stop-the-world • Edge cases continue to improve and push more concurrent operations • Still some stop-the-world pauses in modern GCs but getting smaller

Slide 25

Slide 25 text

©2020 Alex Blewitt Thank you https://alblue.bandlem.com https://twitter.com/alblue https://github.com/alblue https://vimeo.com/alblue https://speakerdeck.com/alblue