garbage collection
DESCRIPTION
TRANSCRIPT
The Purpose of Garbage Collection
Manual memory management is too tiresome and error-prone.
Memory leaks.Dangling pointers.
GC frees programmer to focus on more important issues.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 2 / 9
A Few Definitions
Aim: to free objects that will not be used anymore.
That is computationally impossible.
Reachability: transitive closure of pointers starting with the root set(all global and local variables).
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9
A Few Definitions
Aim: to free objects that will not be used anymore.
That is computationally impossible.
Reachability: transitive closure of pointers starting with the root set(all global and local variables).
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9
A Few Definitions
Aim: to free objects that will not be used anymore.
That is computationally impossible.
Reachability: transitive closure of pointers starting with the root set(all global and local variables).
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 3 / 9
Reference Counting
Each object has a counter.
Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).
When the counter reaches zero, free the object.
Disadvantages: fairly big overhead, can not deal with cycles.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9
Reference Counting
Each object has a counter.
Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).
When the counter reaches zero, free the object.
Disadvantages: fairly big overhead, can not deal with cycles.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9
Reference Counting
Each object has a counter.
Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).
When the counter reaches zero, free the object.
Disadvantages: fairly big overhead, can not deal with cycles.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9
Reference Counting
Each object has a counter.
Reference assignment → decrease the counter of the old object andincrease the counter of the new one (if they exist).
When the counter reaches zero, free the object.
Disadvantages: fairly big overhead, can not deal with cycles.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 4 / 9
Mark and Sweep
Each object has a mark.
Once in a while, go through transitive closure of the root set andmark all objects along the way.
Then free all unmarked objects.
Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9
Mark and Sweep
Each object has a mark.
Once in a while, go through transitive closure of the root set andmark all objects along the way.
Then free all unmarked objects.
Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9
Mark and Sweep
Each object has a mark.
Once in a while, go through transitive closure of the root set andmark all objects along the way.
Then free all unmarked objects.
Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9
Mark and Sweep
Each object has a mark.
Once in a while, go through transitive closure of the root set andmark all objects along the way.
Then free all unmarked objects.
Disadvantages: fragmentation, locality of reference, cost isproportional to the size of available memory.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 5 / 9
Mark and Compact, Mark and Copy
The same as mark and sweep except objects are moved (copied) sothat they are next to each other.
Disadvantages: costly when there is a large number of survivors.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6 / 9
Mark and Compact, Mark and Copy
The same as mark and sweep except objects are moved (copied) sothat they are next to each other.
Disadvantages: costly when there is a large number of survivors.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 6 / 9
Incremental Collectors
Stop the world algorithms disrupt the execution of the program fortoo long.
It is better to collect garbage by increments.
However, incremental algorithms must accommodate for possiblechanges in the object graph.
Two basic ways: read barrier and write barrier.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9
Incremental Collectors
Stop the world algorithms disrupt the execution of the program fortoo long.
It is better to collect garbage by increments.
However, incremental algorithms must accommodate for possiblechanges in the object graph.
Two basic ways: read barrier and write barrier.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9
Incremental Collectors
Stop the world algorithms disrupt the execution of the program fortoo long.
It is better to collect garbage by increments.
However, incremental algorithms must accommodate for possiblechanges in the object graph.
Two basic ways: read barrier and write barrier.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9
Incremental Collectors
Stop the world algorithms disrupt the execution of the program fortoo long.
It is better to collect garbage by increments.
However, incremental algorithms must accommodate for possiblechanges in the object graph.
Two basic ways: read barrier and write barrier.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 7 / 9
Generational Collectors
Most objects live a very short time.
GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.
Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.
The root set for a given region has to include references from regionswith more mature objects.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9
Generational Collectors
Most objects live a very short time.
GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.
Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.
The root set for a given region has to include references from regionswith more mature objects.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9
Generational Collectors
Most objects live a very short time.
GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.
Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.
The root set for a given region has to include references from regionswith more mature objects.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9
Generational Collectors
Most objects live a very short time.
GC among younglings has much higher efficiency—i. e. it lastsshorter and yields more free space.
Therefore, the whole memory is divided into regions, objects arepropagated through regions based on their age.
The root set for a given region has to include references from regionswith more mature objects.
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 8 / 9
See
Paul Wilson. Uniprocessor Garbage Collection Techniques. MemoryManagement. http://dx.doi.org/10.1007/BFb0017182
Michal Pıse (CTU in Prague) Object Prgrmmng L. 11: Garbage Collection December 7, 2010 9 / 9