Garbage Collection 1 What is Garbage Collection What
Garbage Collection 1
What is Garbage Collection? • What is it? • What are techniques for handling it? Garbage Collection 2
Garbage Collection is … • Automatic Memory Management • Memory recycling • When some memory is occupied by an object that is no longer needed, then it can be reclaimed. • Other maintenance: compacting Garbage Collection 3
Motivations • Ease programmer burden • Security and safety • At the cost of: – Performance – Control Garbage Collection 4
Two Goals Detect objects no longer needed Reclaim space Garbage Collection 5
Garbage Collection Methods Many mechanisms exist for GC (see Web reading) – – Reference counting Tracing collectors (e. g. , mark and sweep) Copying collectors Generational collectors Garbage Collection 6
Reference Counting Keep a count of all references to an object. When it’s zero, remove the object – Problems: Cycles – Every store to object memory requires incrementing/decrementing (can defer with things on stack) Not a common technique anymore Garbage Collection 7
Tracing Collectors Trace from root object(s) to everything, marking what’s accessible in the object header someplace – Sweep away everything else – Hence “mark and sweep” Garbage Collection 8
Reducing memory fragmentation Object memory can become fragmented with continual garbage collection. In sweep phase, memory fragmentation can be reduced. – Compacting on the fly – Stop and copy strategy, using separate memory areas Maintaining valid references is key to successful compacting strategy. Garbage Collection 9
Generational Scavenging Two facts about OO programs: 1. Most objects have short lives. New objects tend to go away quickly. 2. Most programs have some long-lived objects Fact 1 means you need to do GC often. Fact 2 means that copying can be inefficient Deal with objects in generations. Move from eden to survivor eventually to old or tenured – Reduces amount of memory space to mark-sweep – By David Ungar, father of Self (Fastest O-O language with all-object semantics) Garbage Collection 10
Garbage Collection in Squeak Combination of generation scavenging and mark-sweep • Two-generation scavenger – Normally, just does generation scavenging, but a full gc also does mark-sweep – That’s why it takes two Smalltalk garbage. Collect to really clear out all of memory On a 400 Mhz Powerbook, 3 ms/second for scavenger, 400 ms for full gc Garbage Collection 11
What if you want to interfere with GC? Can always fix it: Object. Memory Can also use Weak. Arrays – Weak. Arrays don’t prevent object reclamation – But if the last reference is from a Weak. Array, you can do something first – You appoint an executor and finalize methods (see Weak. Registry) Garbage Collection 12
Java’s Garbage Collection Particular algorithm not specified in JVM spec – Exactly like Squeak Java 1. 1 used mark-sweep and object tables Java Hot. Spot VM uses generational scavenging and mark-sweep with direct pointers – Default: Two generations Optionally: Three generations (“train”) for fewer gc pauses – Just like Squeak Garbage Collection 13
- Slides: 13