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
Garbage collection(GC) in JavaScript?
Memory management in JavaScript is much easier than in low-level languages like C or C++. Unlike low-level languages, JavaScript automatically detects which objects will be needed in later cases and which will be garbage that is occupying memory without any reason. In this article, we shall discuss how garbage collection (GC) works in JavaScript. There are a few important parts that we are going to discuss in detail:
Reachability from root
Interlinked Objects (interconnections between different objects)
Unreachable Collections (Broken link)
Reachability
The first concept of memory management is reachability. In this mode, the garbage collector tries to search for reachable values from some point. When a value or memory location is reachable from some point then that will not be removed. An object can be reachable from some location directly or indirectly. There are a few possible ways to determine whether an object is reachable or not:
When a function is executing currently, then its local variables and parameters are directly reachable.
If some other functions can be called from the currently executing function can also be reachable. Not only that function, the parameters and local variables of these functions in the chain will be reachable.
Global variables are always reachable
These directly reachable objects are called root objects. When some other values which are reachable from the roots are also marked as reachable. For instance, consider we have an object ob1, as our global object. As ob1 is global, it is root. Now there is another reference in ob1, which is pointing to ob2. The second object ob2 is not directly reachable, but it is reachable from root object ob1. So, it will also be marked as reachable. When some object becomes unreachable, then a background low-priority process called Garbage Collector cleans these unreachable objects from the memory.
Example: Simple Object Reference
The following example demonstrates basic reachability with a simple object reference:
let company = {
name: "Tutorialspoint"
};
console.log("Company object created:", company.name);
Company object created: Tutorialspoint
The figure depicts a company object pointing to an object which stores a string variable called "Tutorialspoint". Now this object is reachable from the reference named "company". At this moment, if we set the company to null, the reference will be lost.
company = null;
console.log("Reference removed, object becomes unreachable");
Reference removed, object becomes unreachable
After setting company = null, the object is unreachable. So the garbage collector will remove this part in its turn.
Example: Multiple References
Let us see another example, where we are using two references. Consider the following code where the company object is referenced by another variable called ob:
let company = {
name: "Tutorialspoint"
};
let ob = company;
console.log("Two references point to same object");
console.log("company.name:", company.name);
console.log("ob.name:", ob.name);
company = null;
console.log("After setting company = null");
console.log("ob.name still accessible:", ob.name);
Two references point to same object company.name: Tutorialspoint ob.name: Tutorialspoint After setting company = null ob.name still accessible: Tutorialspoint
So the object is referenced by another link, named 'ob'. In other words, the object is reachable through 'ob'. That's why the garbage collector will not remove it from the memory.
Interlinked Objects
JavaScript objects can be linked together with multiple objects. Or we can say they can be interlinked with some similar objects. In that scenario, the structure becomes more complex. Let us see one example to understand interlinked objects.
function createParentChild(dadName, childName) {
let dad = { name: dadName };
let child = { name: childName };
// Create bidirectional references
dad.child = child;
child.dad = dad;
return {
father: dad,
daughter: child
};
}
let parentChild = createParentChild("Bob", "Alice");
console.log("Father name:", parentChild.father.name);
console.log("Child name:", parentChild.daughter.name);
console.log("Child's dad:", parentChild.daughter.dad.name);
Father name: Bob Child name: Alice Child's dad: Bob
This example shows how two objects are interlinked with bidirectional references. Both objects reference each other through 'dad' and 'child' properties.
Unreachable Island
The third case can be the unreachable island. Consider the same example shown in the previous section. Now if we remove the link from the parent object, it will separate the entire object structure:
// After creating the parent-child structure above
parentChild = null;
console.log("Parent reference removed - objects form unreachable island");
console.log("Both father and child objects will be garbage collected");
Parent reference removed - objects form unreachable island Both father and child objects will be garbage collected
The parent link is removed, so that the entire object and father, and child objects are unreachable. They are forming an unreachable island even though they still reference each other internally.
How Garbage Collection Works
JavaScript uses a mark-and-sweep algorithm for garbage collection:
Mark phase: Starting from root objects, the GC marks all reachable objects
Sweep phase: All unmarked objects are considered garbage and removed from memory
Compact phase: Memory is reorganized to reduce fragmentation
Conclusion
JavaScript's automatic garbage collection manages memory efficiently by tracking object reachability from root references. Understanding how GC works helps developers write better code and avoid memory leaks in complex object relationships.
