Chapter 6: Runtime System

Reference Count

. Keep track of number of blocks which point directly to the present block

. If count drops to 0 then block can be de-allocated

. Maintaining reference count is costly

- assignment p:=q leads to change in the reference counts of the blocks pointed to by both p and q

. Reference counts are used when pointers do not appear in cycles

One convenient way is to keep track of the number of blocks pointing to any given block. When this number reaches zero, we can see that the block can no longer be reachable and so it has become garbage. This method is easy to implement but can be costly in time as for the assignment p:=q, the reference count of block which was previously pointed to by p goes down by one, while that of q goes up by one. Reference counts are best used when the graph of blocks pointing to blocks is guaranteed to be a forest (can not contain cycles). That is because if there is a cycle, which is not reachable, then the reference count of all blocks in the cycle will be non-zero, even though they are all unreachable. So they will never get de-allocated, thus leading to space wastage.