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 Mark and Sweep algorithm in JavaScript?
Mark and Sweep Algorithm
The Mark and Sweep algorithm is JavaScript's primary garbage collection method that identifies objects "which are unreachable" rather than objects "which are no longer needed". This algorithm is an improvement over the Reference-counting algorithm and solves the circular reference problem.
How Mark and Sweep Works
The algorithm operates in three important steps:
- Root Identification: The algorithm starts from "roots" - global variables accessible in the code. In browsers, the window object acts as the primary root.
- Marking Phase: The algorithm traverses from roots to find all reachable objects. Reachable objects are "marked" while unreachable objects remain "unmarked".
- Sweep Phase: All unmarked (unreachable) objects are garbage collected and their memory is freed.
Mark Phase
In the mark phase, the algorithm identifies which elements are reachable from the root. Consider this example:
var obj1 = {
pro1: "hello" // marked because it can be reached from root
};
console.log("Object created and reachable from global scope");
Object created and reachable from global scope
When the object reference is set to null, the marking changes:
var obj1 = {
pro1: "hello"
};
obj1 = null; // null is marked (reachable), "hello" becomes unmarked (unreachable)
console.log("obj1 is now null, original object is unreachable");
obj1 is now null, original object is unreachable
Sweep Phase
The sweep phase removes all unmarked (unreachable) objects from memory. In our example above, the object containing "hello" becomes eligible for garbage collection since no references point to it anymore.
The Mark and Sweep algorithm is also called a "tracing garbage collector" because it traces the entire collection of objects that are directly or indirectly accessible by the program.
Solving Circular References
Unlike reference counting, Mark and Sweep handles circular references effectively:
function createCircularReference() {
var obj1 = {};
var obj2 = {};
obj1.ref = obj2; // obj1 references obj2
obj2.ref = obj1; // obj2 references obj1 (circular reference)
console.log("Circular reference created");
return "Function completed";
}
console.log(createCircularReference());
// After function returns, both obj1 and obj2 become unreachable
// and will be garbage collected despite the circular reference
Circular reference created Function completed
Comparison with Reference Counting
| Aspect | Reference Counting | Mark and Sweep |
|---|---|---|
| Circular References | Cannot handle | Handles effectively |
| Memory Recovery | Immediate | Periodic |
| Performance Impact | Continuous overhead | Periodic pause |
Limitations
While Mark and Sweep is highly effective, it has some limitations:
- Manual memory management is not possible - developers cannot explicitly trigger garbage collection
- The algorithm runs periodically, which may cause brief pauses in program execution
- Objects must be explicitly made unreachable for garbage collection to occur
Conclusion
Mark and Sweep is JavaScript's primary garbage collection algorithm that effectively manages memory by tracing reachable objects from roots. It solves the circular reference problem that plagued earlier algorithms and automatically handles memory cleanup without developer intervention.
