Next
Previous
First
Last
Index
Heaps and Garbage Collection
Alan Kaminsky
Home Page
4. Reference Counting Garbage Collection
4.1. Reference Counters
Block header
nWords = The number of words in the block, block size (header + data area)
refCount = The number of pointers pointing to this block (initialized to 0 at allocation)
To assign a pointer, old value = block R, new value = block S:
Increment S.refCount
Delete (R)
Pointer = S
To delete block P:
Decrement P.refCount
If P.refCount == 0
For every pointer Q in Ps data area
Delete (Q)
Put P on the free list
Pros
Very simple, non-compacting garbage collection
Heap maintenance spread throughout program execution (instead of suspending the program when the garbage collector runs)
Cons
Extra word in block header to hold reference count
Fragile; if you forget to adjust reference counts on any pointer assignment (including passing pointers as subroutine arguments), disaster can happen
Major problem:
Cannot garbage collect circularly linked data structures
Next
Previous
First
Last
Index
Heaps and Garbage Collection
Alan Kaminsky
Home Page
Copyright © 2001 Rochester Institute of Technology. All rights reserved. Last updated 06-Dec-2001. Please send comments to ark
@
cs.rit.edu.