• 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
(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
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
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
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
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
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
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
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
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
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
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
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