Early JVM's used reference counting to determine objects no longer in use. In reference counting, an objects reference count is initially set to one when it is instantiated and it's reference is assigned to a variable. Every time a reference of that object is assigned to another variable, the count is incremented by one. When a variable holding the reference goes out of scope, the count of the object is decremented by one. An object is eligible for garbage collection when it's reference count becomes zero.
Let's see how reference counting algorithms work, with a simple animation.
Right click in the region below and select 'play' to start the animation.
Advantages:
It is very simple to implement.
The JVM does not need to be paused when the garbage collector is running.
Disadvantages:
There is an extra overhead since the reference count of objects needs to be updated very often.
Cannot detect cycles.
A cycle is formed when two objects point to each other. Even if both these objects are not used anywhere else, they will not be garbage collected since their reference count is one.
Some of the disadvantages of reference counting algorithms are handled by tracing algorithms.
Discuss this post in the learning forum.
Commercial Links
- Buy programming books from our Amazon.com powered storefront.
- Earn as you browse. Sign up for Algoco.
Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net
Comments