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.

Examples

The above-mentioned idea is shown using a code and some diagrams.

Code

let company = { name: "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.

Code

company = null;


After setting company = null, the object is unreachable. So the garbage collector will remove this part in its turn.

Let us see another example, where we are using two references. Let us consider the following code where the company object is referenced by another variable called ob.

Code

let company = { name: "Tutorialspoint" }; let ob = company;

Then, if we set company = null, the state will be like below−

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.

Example

Code

function parent(dad, daughter) { dad.daughter = daughter; daughter.dad = dad; return { father: dad, child: daughter } } let parent = parent({ name: "Bob" }, { name: "Alice" });

This example is showing the parent() function is creating two objects father and child with the names “Bob” and “Alice” these two objects are related to each other by the ‘dad’ and ‘daughter’ relationship. In the following diagram, we can visualize this situation. It tells, that these two objects are interlinked.

At this moment, if we remove few connections like−

Code

delete parent_child.father delete parent_child.child.dad


After removing these two links, the father (Bob) has no incoming link. The garbage collector will remove the whole father object from the memory. This idea is depicted in the following figure.

Unreachable Island

The third case can be the unreachable island. Consider the same example shown in the previous section (Interlinked Objects). Now if we remove the link from the parent object, it will separate the entire object as shown in the following diagram−


The parent link is removed, so that the entire object and father, and child objects are unreachable. They are forming an unreachable island.

Code

parent = null;

Conclusion

Like any other language, memory management is a very important task, and most importantly, clearing the unused memory blocks is necessary to write better and more efficient code. JavaScript automatically collects the information of the unmercenary memory blocks and removes them from the memory. The garbage collector searches for reachability from the root object to determine whether an object will be used in the future or not. For interlinked objects, it searches whether an object has no incoming edge, then that object cannot be reached, so it removes that object from the memory.

Updated on: 22-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements