Taking out the Garbage: an overview of garbage collectors in Java, and what new garbage collectors bring to the JVM.
©2020 Alex BlewittTaking out the garbageAn overview of GCs in the JVM
View Slide
©2020 Alex BlewittGenerational Garbage CollectionFrom Young to Old• Generational Hypothesis: Most objects die young• Have a young generation which is cleared periodically• Promote long-lived objects to older generationsEdenSurvivorOldYoung
©2020 Alex BlewittEden• 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 collectionEdenTLAB TLAB TLAB TLAB TLAB TLAB TLAB
©2020 Alex BlewittAllocations 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
©2020 Alex BlewittMinor 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 spaceEdenOldSurvivor
©2020 Alex BlewittMinor 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 spaceEdenToOldFrom
©2020 Alex BlewittObject 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
©2020 Alex BlewittGarbage 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
©2020 Alex BlewittApplication Pause TimebcdaApplicationThreads
©2020 Alex BlewittApplication Pause TimeabcdApplicationThreadsSafepointTriggerSafepointStart
©2020 Alex BlewittApplication Pause TimeabcdaaaaApplicationThreadsSafepointTriggerSafepointStartSafepointEndGCThreadsGCThreads GC timeApplication Stopped TimeTime ToSafepoint
©2020 Alex BlewittParallel GCabcdaaaaApplicationThreadsGCThreadsGCThreads
©2020 Alex BlewittSerial GCabcdaApplicationThreadsGCThreadsGCThreads
©2020 Alex BlewittConcurrent GCabcdaApplicationThreadsGCThreadsGCThreadsa
©2020 Alex BlewittConcurrent Parallel GCabcdaApplicationThreadsGCThreadsGCThreadsaaaa
©2020 Alex BlewittRoot 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
©2020 Alex BlewittRegional 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
©2020 Alex BlewittGarbage 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)ESOHESOE S OHOESOHSOO OHO
©2020 Alex BlewittShenandoah 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
©2020 Alex BlewittShenandoah algorithmhttps://wiki.openjdk.java.net/display/shenandoah/Main
©2020 Alex BlewittZGC 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
©2020 Alex BlewittZGC memory mappingMemMemMemMemOOOOOReferences are repaired on loadMemory mapped multiple timesMemory split into pageslike G1/Shenandoah regions
©2020 Alex BlewittComparisonG1 Shenandoah ZRegional ✅ ✅ ✅Generational ✅ Young STW ✅ Concurrent Evacuation ✅ ✅x86_32 ✅ ✅ x86_64 ✅ ✅ ✅Compressed Oops ✅ ✅ Return Memory 12+ ✅ 13+Memory 2M-? 8M-? 8M-16TMax Pause ~1s ~1ms ~1msSupported 8-14 8u,11u,12-14 11-148u and 11u BackportsBoth Shenandoah and ZGC need UnlockExperimentalVMOptionsbut plan to graduate to non-experimental in JDK 15
©2020 Alex BlewittSummary• 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
©2020 Alex BlewittThank you https://alblue.bandlem.com https://twitter.com/alblue https://github.com/alblue https://vimeo.com/alblue https://speakerdeck.com/alblue