190 likes | 563 Views
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
E N D
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. Figure 1: Example objects Y Object i Object j 1 2 X Z
Reference Counting Step 0: Start Object i 1 X Object j 1 Y Step 1: Y = Object i; Object i 1+1 X Object j 1-1 Y
Reference Counting Step 3: Check right hand side Object i 2 X Object j 0 Y Step 4: Finish assignment Object i 2 X Y
Reference Counting • Advantages • Easy to implement • Does not wait till memory is exhausted • Disadvantages • ...
Reference Counting • Assignment operation gets slower 1 1 1 1 X 1 1 1
Reference Counting • Assignment operation gets slower 1 1 1 0 X 1 1 1
Reference Counting • Does not work with circular structure Object i 2 X Object j 1
Reference Counting • Does not work with circular structure Object i 1 X Object j 1
Stop and Copy Allocation Pointer Old Space New Space Allocation Pointer A B New Space C D E
Stop and Copy Allocation Pointer Root set A B New Space C D E Scan Pointer
Stop and Copy Allocation Pointer Scan Pointer Root set A B C D E A Allocation Pointer Scan Pointer Root set B A B C D E A
Stop and Copy Root set A B E Old Space New Space
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.