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