Reference Counting

For a reference counting garbage collection strategy, looks at each object and its count of incoming pointers. When a reference count drops to 0, memory is freed.

Poor in both space and time complexity.

For x=y;
 
// compiler generated code before user’s assignment
// decrement the reference count of whatever object is currently
// referred to by variable x (should check for null first)
x.refcount−−;
// collect the object referred to by x if its counter has gone
// down to zero
if (x.refcount == 0) { delete x; }
// user’s assignment
x = y;
// compiler generated code after user’s assignment
// increment reference counter of object referred to by x
// (which is now referred to by both x and y)
x.refcount++;