UniProcessor GarbageCollection Techniques
Paul R. Wilson
University of Texas
Presented By Naomi Sapir
Tel-Aviv University
Overall Goal
Introduction of garbage collectors foruniprocessors.
Clarify basic issues in the field.
Motivation
Modular programming.
Unreclaimed memory leads to slow memoryleaks.
Reclaiming too soon may causeunpredictable results.
GC should be built into a languageimplementation.
Two Phase Abstraction
Distinguish live objects from garbage byterms of root set.
- reference counting
- mark sweep
- copying
Reclaim Garbage storage.
Root Set
Global variables
local variables in the activation stack.
Registers
Live object: Any object reached from theRoot Set.
What is Garbage ?
Reference Counting
Unreclaimable Cycle
Cons
Conservative approximation of trueliveness.
Efficiency cost proportional to the numberof objects allocated in runtime.
- a real pointer points to another object
- short lived stack variables
Fragmentation.
Pros
Used in distributed systems combined withother techniques.
Mark-Sweep Collection
Root
Page
Object
Mark-swept
Cons
Fragmentation - difficult to allocate largeobjects.
Locality of reference: interleave of differentages causes many page swaps.
Cost: proportional to the heap size.
Mark Compact CollectionCompact after mark
Pros
Solves fragmentation.
Saves locality.
Cons
Cost: Several passes over the data:
Mark, compute new locations,
update pointers and move theobjects.
Copying Garbage Collection
Before garbage collection
After garbage collection
Cheney breath-first copying
Efficiency of CopyingCollection
Proportional to the live data duringcollection.
Decrease of collection frequency, decreasescollection effort.
Need increased heap memory.
Objects that die before GC needn’t becopied.
Basic Techniques - Conclusions
High performance systems use hybridtechniques.
Copy collectors use a separate large objectsarea.
In-place collectors (mark-sweep, treadmill)are conservative in respect to untypedobjects, a copying collector must identifypointers.
Problems with basic GC
Not large memory, causes excessive paging.
A copying collection might cause paging.
Locality in cache memory is important.
Time consuming, not usable for real timeapplications.
Incremental Tracing Collectors
Incremental Tracing Collectors
Incremental tracing for garbage detection.
The running program may mutate the graphof reachable objects.
- keep track of changes.
- floating garbage.
Tricolor Marking
              Before                        After a violation-D isnotreachable
 
Incremental Approaches
Coordinates the collector with the mutator.
Read barrier
A mutator access a pointer to a white object,colors the object grey.
Write barrier - a direct method
- Traps the write of a white pointer into a blackobject.
- Traps the death of a pointer before it isreached by GC.
Baker’s Incremental Copying
The best known real-time garbage  collector.
Free list(tospace), Live list (fromspace).
Object: two pointers (next,prev) and a color(for the set).
Fast allocation: copying in Cheney fashion.
Read Barrier.
Baker’s Incremental Copying(cont’)
Tricolors:
- Black: Scanned area in tospace
Grey: copied but not scanned.
White: unreached objects in fromspace.
Use scan-pointer on unscanned area oftospace, and move referred-to objects fromfromspace.
Baker’s Incremental Copying(cont’)
Rule: Scanned objects in tospace(black)cannot point to objects in fromspace(white).
If a mutator tries to access a  pointer fromthe fromspace (white), the referent is copiedinto the tospace (grey) before the access.
Allocation of new objects during GC isdone in tospace, they are live - black.
Baker’s Incremental Copyingimplementation
Rate of copy is tied to the rate of runtimeallocation.
Read barrier: compiled in software or
implemented by hardware checks and/ormicrocode routines (Lisp Machines)
Order of 20% time overheads.
The Treadmill (Baker)
Non-copying.
Doubly linked lists.
The Treadmill (cont’)
Duringallocation
The Treadmill Conservatism
Allocated objects are marked live, but mightdie before the collection finishes.
Pre-existing object marked live, might dieafter being reached.
If the mutator destroys all the grey objectsthat point to a white object, although thewhite object will not be reachable by thecollector, its memory will be reclaimed.
Snapshot-at-BeginningWrite-Barrier (Yuasa)
Cheaper then read barrier, as heap writesare less common then heap reads.
The graph of reachable objects is fixed fromthe beginning.
All objects are accessed by the GC duringcollection (saves overwritten values).
more conservative then Baker,all pointersretained, no free during GC.
Tricolor Marking
              Before
D isreachable
Incremental UpdateWrite-Barrier(Dijkstra)
Heuristically retain live objects at the end ofGC.
Objects that die during GC and beforereached by GC, may be reclaimed.
Records a pointer that escapes into analready reached object (black   white) (grey  white)
Incremental UpdateWrite-Barrier(Dijkstra) cont’
New objects are allocated white: short livedobjects will not be traversed early, but willbe reclaimed quickly (advantage).
Comparison of Incremental GCs
Generational GarbageCollection(copying)
BeforeGC
After GC
Generational Garbage Collection
Does not copy all live data at acollection.
Does not copy old objects repeatedly.
New objects are allocated in the New Gen.
When full, New Gen only is scavenged,then old objects are copied to the Old Gen.
Include a pointer Old Gen   New Gen inthe Root Set.
Generational Garbage CollectionCont’
Copy Collector: all pointers to movedobjects are updated.
Conservative true liveness, not all pointersOld Gen   New Gen are live, they willfloat until the Old Gen will be scavenged.
Generational Garbage CollectionCont’
Newer generations are usually smaller thenolder, so scanning them is faster.
Better locality.
Record of intergenerational pointers is nottied to the rate of object creation, but stillmight slow the program.
Conclusions
Generational techniques reduce cost asobjects tend to die fast.
Generational techniques with write barriercan support incremental update collection.
We studied several kinds of GCs.
Most important characteristics of GCs.
Constant factors of cost (locality effects).
Understanding current research.