Saturday 19 July 2014

Reference Counting Approach

Reference Count
The number of active references to an object.

Each Object associate with a reference count. If an object reference count reaches to zero, then it is eligible for Garbage Collection. It is incremental technique, through out your Application execution, Garbage collector keeps track of reference count of an object.

Reference Counting Algorithm is simple to implement. Many Languages like Smalltalk, LISP and Java etc., adapt this technique.

When a new object created and has reference, then the reference count for that object is one. Whenever new reference point to this object, then the reference count is incremented by one. Whenever a reference to this object is unreferenced, then the reference count is decremented by one. Whenever the reference count of this object reaches to zero, then this object is eligible for Garbage Collection, so Garbage Collector reclaim the memory used by this object.


Advantages
  1. Easy to implement.
  2. Overhead is distributed across the Application execution.
  3. We can recycle the garbage memory instantly and incrementally.

Disadvantages


As you see the above figure, Let us assume, node1 is the head pointer to the linked list.

Node Reference Count
Node 1 1
Node 2 2 (Since Node1 and Node3 are pointing to Node2)
Node 3 1

If Node1 release the reference to Node2, then reference count for Node 2 is 1 and Node2 and Node3 are unreachable and becomes garbage. But as per the reference count algorithm, object is eligible for garbage collection, if the reference count is zero. These kind of Circular list problems are not solved by Reference Count algorithm.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment