260 likes | 494 Views
CS 480/680 – Comparative Languages. Garbage Collection and Memory Management. Motivation for GC. int mysub(times) { myClass *myObject; myObject = new myClass; for(int i=0; i<times; i++) { int a = myObject.firstMethod(); int b = myObject.secondMethod(); } return(a/b); }.
E N D
CS 480/680 – Comparative Languages Garbage Collection and Memory Management
Motivation for GC int mysub(times) { myClass *myObject; myObject = new myClass; for(int i=0; i<times; i++) { int a = myObject.firstMethod(); int b = myObject.secondMethod(); } return(a/b); } What’s wrong with this code? Garbage Collection
Memory Management • Explicit memory management • C++ new/delete • C malloc/free • Garbage collection • Java new Class • Ruby Class.new • Perl • Smalltalk • Etc… Garbage Collection
What is Garbage Collection • Ideally, the garbage collector will find all objects that will not be accessed in the future. • Of course, this is impossible, but a good guess is those objects that have no references to them remaining in scope. • When to invoke GC? • When memory is low • When the local scope changes • When the system is idle • When a reference is destroyed • Others? Garbage Collection
Advantages of Garbage Collection • Easier on the programer • Create objects when needed • No need to de-allocate space • No need for destructors and other special memory considerations • Less errors • Less memory leaks • No dangling pointers Garbage Collection
GC Strategies • Two of the most common strategies are: • Tracing • Includes the very common Mark & Sweep method • Reference Counting Garbage Collection
Tracing Garbage Collectors • Start with all objects accessible to the main program (called roots) • Registers • Stack • Instruction pointer • Global variables • Find all objects reachable from the roots Garbage Collection
Roots $6FE8 D0 $6FEA Globalvariablelist D1 ptr2 $6FEC ptr1 $6FEE D2 var2 $6FF0 $6FF2 var1 … Old A6 $6FF4 A0 Old A6 $6FF6 $6FF8 rtrn A1 PC $6FFA rtrn … $6FFC param A7 param $6FFE ? $7000 Garbage Collection
Reference Chains $6FE8 myObject $6FEA ptr2 $6FEC acctList ptr1 $6FEE var2 $6FF0 $6FF2 var1 Old A6 $6FF4 Old A6 $6FF6 $6FF8 rtrn Value $6FFA rtrn Next $6FFC param param $6FFE ? $7000 Garbage Collection
Tri-color Algorithm • Every object is the system is “marked” in one of three colors: • White – candidate for recycling (condemned) • Black – Safe from recycling; No references to objects in the white set • Grey – Safe from recycling; May refer to objects in the white set Garbage Collection
Initialization • White set – everything except the roots • Black set – empty • Grey set – the roots (already safe from recycling) Garbage Collection
Tracing Repeat: • Pick an object from the grey set • Grey all white objects that this object refers to • Move the object to the black set Until: The grey set is empty Now recycle all objects remainingin the white set. Garbage Collection
Example Step (1) $6FE8 $6FEA ptr2 $6FEC Value ptr1 $6FEE Next var2 $6FF0 $6FF2 var1 Old A6 $6FF4 Old A6 $6FF6 Value $6FF8 rtrn Next $6FFA rtrn $6FFC param param $6FFE ? $7000 Garbage Collection
Example Step (2) $6FE8 $6FEA ptr2 $6FEC Value ptr1 $6FEE Next var2 $6FF0 $6FF2 var1 Old A6 $6FF4 Old A6 $6FF6 Value $6FF8 rtrn Next $6FFA rtrn $6FFC param param $6FFE ? $7000 Garbage Collection
Example Step (3) $6FE8 $6FEA ptr2 $6FEC Value ptr1 $6FEE Next var2 $6FF0 $6FF2 var1 Old A6 $6FF4 Old A6 $6FF6 Value $6FF8 rtrn Next $6FFA rtrn $6FFC param param $6FFE ? $7000 Garbage Collection
Example Step (4) $6FE8 $6FEA ptr2 $6FEC Value ptr1 $6FEE Next var2 $6FF0 $6FF2 var1 Old A6 $6FF4 Old A6 $6FF6 Value $6FF8 rtrn Next $6FFA rtrn $6FFC param param $6FFE ? $7000 Garbage Collection
An Important Observation • Note that, by following this method, an object in the black set can never refer to an object in the white set • When an object is moved to the black set, all the objects it points to are moved to the grey set. • At the end, all of the objects in the white set have no live references to them, and can safely be removed. Garbage Collection
Variations of Tracing GC • To move or not to move… • Mark & Sweep – keep a few bits with each object to indicate if it is in the black, grey, or white sets. After marking, recycle every object in memory in the white set. • Copying GC – relocate objects in the black set to a “safe” area of memory, then recycle everything else • This is nice for creating good locality of reference! • Also reduces fragmentation. • Can be slower. Garbage Collection
More Variations • Can the collector identify which parts of objects are references (pointers) and which are not? • Yes: “precice” GC • No: “conservative” GC • What if pointers are encrypted, scrambled, or stored in some other “funny” way? Value Next Value Next Garbage Collection
Still More Variations • Can the GC mechanism run incrementally? • No: stop the rest of the system, do GC, then start up again • Yes: interleave their work with work units from the rest of the system • Can the GC mechanism run in a parallel thread? • Note: All methods must at least scan the root set all at once • Why? Garbage Collection
Generational GC • It is not strictly necessary to place everything (except the root objects) in the white set • Faster GC can be performed by limiting the size of the white set • What should go there? • Statistically speaking, the newest objects are also the most likely to go out of scope soon • Referred to as infant mortality or the generational hypothesis • Divide run time into generations, only put objects created in the current generation into the white set Garbage Collection
Disadvantages of Tracing GC • Can be invoked at any time, and can be slow • Not a good thing for Real Time (RT) computing • The GC thread violates locality of reference by intentionally looking at memory areas that haven’t been accessed recently • So what? Garbage Collection
Open Research • Is all memory that is reachable still in use? • Consider ARGV & ARGC. Usually read, parsed, and then ignored for the rest of the process. Do we really need to keep it around? • Research area: finding objects that will never be used again… Garbage Collection
Reference Counting • Keep a reference count with every object • Advantages: • Easy to implement • Fast and incremental GC • Disadvantages: • Must update reference count whenever a reference is created or deleted: SLOW • Need extra space in every object • Does not reclaim space if there are reference cycles (as in a doubly linked list) Garbage Collection