Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain in detail about Reference-counting garbage collection in JavaScript?
Reference-counting garbage collection is the simplest garbage collection algorithm used in JavaScript. This algorithm monitors objects and removes those that have no references pointing to them. An object becomes eligible for garbage collection when its reference count drops to zero.
How Reference-Counting Works
The algorithm maintains a count of how many references point to each object. When a reference is created, the count increases. When a reference is removed, the count decreases. Objects with zero references are immediately marked for garbage collection.
Example: Basic Reference Counting
var obj = {
x: { y: 2 }
};
// 2 objects created. One is referenced by the other as one of its properties.
// Obviously, none can be garbage-collected
console.log("Before reassignment - obj references the main object");
obj = 1; // what was the 'x' property of the object originally in obj
// has zero references to it. It can be garbage collected.
console.log("After reassignment - original object eligible for GC");
Before reassignment - obj references the main object After reassignment - original object eligible for GC
Limitations: Circular References
Reference-counting has a major limitation when dealing with circular references. When objects reference each other in a cycle, their reference counts never reach zero, even when they become unreachable from the main program.
Example: Circular Reference Problem
function createCycle() {
var obj1 = {};
var obj2 = {};
obj1.p = obj2; // obj1 references obj2
obj2.p = obj1; // obj2 references obj1. This creates a cycle.
console.log("Objects created with circular references");
// After function ends, obj1 and obj2 go out of scope
// But they still reference each other, so reference count never reaches 0
}
createCycle();
console.log("Function ended - objects should be garbage collected but may not be");
Objects created with circular references Function ended - objects should be garbage collected but may not be
Comparison with Modern Garbage Collection
| Algorithm | Handles Cycles? | Performance | Used in Modern JS? |
|---|---|---|---|
| Reference-counting | No | Fast for simple cases | No - legacy only |
| Mark-and-sweep | Yes | Good overall | Yes - standard today |
Why It's No Longer Used
Modern JavaScript engines use mark-and-sweep garbage collection instead of reference-counting because:
- It can handle circular references properly
- It's more efficient for complex object relationships
- It prevents memory leaks in scenarios with object cycles
Conclusion
While reference-counting garbage collection is conceptually simple, its inability to handle circular references makes it unsuitable for modern JavaScript engines. Understanding this limitation helps explain why mark-and-sweep became the standard approach.
