garbage collection
DESCRIPTION
TRANSCRIPT
![Page 1: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/1.jpg)
![Page 2: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/2.jpg)
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
![Page 3: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/3.jpg)
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
![Page 4: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/4.jpg)
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
![Page 5: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/5.jpg)
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
![Page 6: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/6.jpg)
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
![Page 7: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/7.jpg)
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
![Page 8: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/8.jpg)
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
![Page 9: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/9.jpg)
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
![Page 10: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/10.jpg)
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
![Page 11: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/11.jpg)
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
![Page 12: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/12.jpg)
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
![Page 13: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/13.jpg)
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
![Page 14: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/14.jpg)
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
![Page 15: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/15.jpg)
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
![Page 16: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/16.jpg)
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
![Page 17: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/17.jpg)
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
![Page 18: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/18.jpg)
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
![Page 19: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/19.jpg)
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
![Page 20: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/20.jpg)
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
![Page 21: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/21.jpg)
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
![Page 22: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/22.jpg)
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
![Page 23: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/23.jpg)
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
![Page 24: Garbage Collection](https://reader034.vdocuments.net/reader034/viewer/2022050919/546e7cc9af7959fa018b6cf6/html5/thumbnails/24.jpg)
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