GARBAGE COLLECTION Student: Jack Chang
Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use objects that can be found.
Reference Counting Reference Count Store reference count in each object For each assignment operation, update reference count If reference count equals 0, we de-allocate the object. X 1 Object i Y 2 Object j Z Figure 1: Example objects
Reference Counting X Y 1 1 Object i Object j X Y Object i Object j Step 0: Start Step 1: Y = Object i;
Reference Counting X Y 0 2 Object i Object j X Y 2 Object i Step 3: Check right hand side Step 4: Finish assignment
Reference Counting Advantages Easy to implement Does not wait till memory is exhausted Disadvantages...
Reference Counting Assignment operation gets slower X
Reference Counting Assignment operation gets slower X
Reference Counting Does not work with circular structure X 1 2 Object i Object j
Reference Counting Does not work with circular structure X 1 1 Object i Object j
Stop and Copy Old SpaceNew Space Allocation Pointer New Space AB CDE Allocation Pointer
Stop and Copy New Space AB CDE Root set Allocation Pointer Scan Pointer
Stop and Copy AB CDE Root set A Scan Pointer Allocation Pointer AB CDE Root set A Scan Pointer Allocation Pointer B
Stop and Copy Old Space New Space ABE Root set
Stop and Copy Advantages Defragment memory during garbage collection The more garbage the faster it will be Disadvantages Need to copy all the reachable objects to new space Need twice as much memory as the program actually uses
At the end The garbage collection is convenient, but... Pauses There are more advanced garbage collection algorithms Concurrent Parallel Thank you.