Garbage Collection in JavaScript - GeeksforGeeks

29 Jun, 2020
This article will explain the concept of garbage collection in JavaScript. In order to understand the need of garbage collection, we must first understand Memory Life Cycle
Memory Life Cycle: The memory life cycle is pretty much the same for any programming language, it has 3 major steps.
Allocate the memory.
Use the allocated memory either to read or write or both.
Release the allocated memory when it is no longer required.
An overview behind garbage collection: The majority of memory management issues occur when we try to release the allocated memory. The main concern that arises is the determination of unused memory resources. In case of the low-level languages where the developer has to manually decide when the memory is no longer needed, high-level languages such as JavaScript use an automated form of memory management known as Garbage Collection(GC).
Garbage Collection: The below section will explain the concepts that are necessary to understand the main garbage collection algorithms and their limitations. The main concept of the algorithms designed for garbage collection is the concept of reference. An object can have a reference to another object if the previous object has access to the latter. For example, a JavaScript object can have an implicit reference(when the reference is to its prototypes) and explicit( when the reference is to its properties values).Below we will explain the algorithms used for Garbage Collection.
Reference-counting garbage collection: This algorithm is considered to be the most basic kind of garbage collection algorithm. What these algorithms do is that rather than determining whether any resource is important or not it scans the memory to determine if an object has any other objects referring to it. An object with zero references is considered to be garbage or “collectible”.Example:javascriptjavascript// Consider the following example  // Declare an objectvar object_1 = {    object_2: {        object_3: 7    }};  // In this example, create two objects// One object is referred by another // as one of its properties. Currently, // none can be garbage collected  // The "object_4" variable is the second// thing that has a reference to the objectvar object_4 = object_1;  // The object that was originally in // "object_1" has a unique reference // embodied by the "object_4" variableobject_1 = 1;  //Reference to "object_2" property of// the object. This object now has 2 // references: 1 as a property,// The other as the "object_5" variable.var object_5 = object_4.object_2;  // The object that was in "object_1" has// now zero references to it. It can be // garbage-collected. However its "object_2"// property is still  referenced by the// "object_5" variable, so it cannot be freed.object_4 = "Geeks For Geeks";  // Now the "object_2" property has no // references to it and hence it can// be garbage collected.object_5 = null;Obstructions: Circular referencesLimitations arise when it comes to circular references. A circular reference occurs when two objects are created with properties that refer each other, thus creating a cycle. The reference-counting algorithm fails to reclaim the these memory resources as each object has at least one reference pointing to them which prevents both the objects from being marked for garbage collection. Circular references are one of the major cause for memory leaks.Below example shows an instance of said case.Example:javascriptjavascriptfunction Demo() {    var one = {};    var two = {};      // one reference to two    one.object = two;      // two reference to one    two.object = one;      return 'circular';}  Demo();Mark-and-sweep-algorithm: This algorithm modifies the problem statement from the “object being no longer needed” to the object being “unreachable”. This algorithm demands a prerequisite of the knowledge of roots which are a set of objects. In JavaScript, a root is a global object. On a regular basis, the garbage collector starts from these roots and finds all the objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will find all the objects that are reachable and mark all the non-reachable objects. Cycles are no longer problem: After the function call returns, the two objects are no longer referenced by any resource that is reachable from the root or global object. Hence, these will be marked as unreachable by the garbage collector and have their allocated memory reclaimed. Some Limitations: The only limitation that can be found is that it is not possible to explicitly or programmatically trigger garbage collector in JavaScript.Hence if there are cases when it would be convenient to manually program when to release memory, there are no provisions in JavaScript to trigger such an event. My Personal Notes
arrow_drop_upSave
Reference-counting garbage collection: This algorithm is considered to be the most basic kind of garbage collection algorithm. What these algorithms do is that rather than determining whether any resource is important or not it scans the memory to determine if an object has any other objects referring to it. An object with zero references is considered to be garbage or “collectible”.Example:javascriptjavascript// Consider the following example  // Declare an objectvar object_1 = {    object_2: {        object_3: 7    }};  // In this example, create two objects// One object is referred by another // as one of its properties. Currently, // none can be garbage collected  // The "object_4" variable is the second// thing that has a reference to the objectvar object_4 = object_1;  // The object that was originally in // "object_1" has a unique reference // embodied by the "object_4" variableobject_1 = 1;  //Reference to "object_2" property of// the object. This object now has 2 // references: 1 as a property,// The other as the "object_5" variable.var object_5 = object_4.object_2;  // The object that was in "object_1" has// now zero references to it. It can be // garbage-collected. However its "object_2"// property is still  referenced by the// "object_5" variable, so it cannot be freed.object_4 = "Geeks For Geeks";  // Now the "object_2" property has no // references to it and hence it can// be garbage collected.object_5 = null;Obstructions: Circular referencesLimitations arise when it comes to circular references. A circular reference occurs when two objects are created with properties that refer each other, thus creating a cycle. The reference-counting algorithm fails to reclaim the these memory resources as each object has at least one reference pointing to them which prevents both the objects from being marked for garbage collection. Circular references are one of the major cause for memory leaks.Below example shows an instance of said case.Example:javascriptjavascriptfunction Demo() {    var one = {};    var two = {};      // one reference to two    one.object = two;      // two reference to one    two.object = one;      return 'circular';}  Demo();
Reference-counting garbage collection: This algorithm is considered to be the most basic kind of garbage collection algorithm. What these algorithms do is that rather than determining whether any resource is important or not it scans the memory to determine if an object has any other objects referring to it. An object with zero references is considered to be garbage or “collectible”.
Example:
javascript
// Consider the following example  // Declare an objectvar object_1 = {    object_2: {        object_3: 7    }};  // In this example, create two objects// One object is referred by another // as one of its properties. Currently, // none can be garbage collected  // The "object_4" variable is the second// thing that has a reference to the objectvar object_4 = object_1;  // The object that was originally in // "object_1" has a unique reference // embodied by the "object_4" variableobject_1 = 1;  //Reference to "object_2" property of// the object. This object now has 2 // references: 1 as a property,// The other as the "object_5" variable.var object_5 = object_4.object_2;  // The object that was in "object_1" has// now zero references to it. It can be // garbage-collected. However its "object_2"// property is still  referenced by the// "object_5" variable, so it cannot be freed.object_4 = "Geeks For Geeks";  // Now the "object_2" property has no // references to it and hence it can// be garbage collected.object_5 = null;
Limitations arise when it comes to circular references. A circular reference occurs when two objects are created with properties that refer each other, thus creating a cycle. The reference-counting algorithm fails to reclaim the these memory resources as each object has at least one reference pointing to them which prevents both the objects from being marked for garbage collection. Circular references are one of the major cause for memory leaks.Below example shows an instance of said case.
Example:
javascript
function Demo() {    var one = {};    var two = {};      // one reference to two    one.object = two;      // two reference to one    two.object = one;      return 'circular';}  Demo();
Mark-and-sweep-algorithm: This algorithm modifies the problem statement from the “object being no longer needed” to the object being “unreachable”. This algorithm demands a prerequisite of the knowledge of roots which are a set of objects. In JavaScript, a root is a global object. On a regular basis, the garbage collector starts from these roots and finds all the objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will find all the objects that are reachable and mark all the non-reachable objects. Cycles are no longer problem: After the function call returns, the two objects are no longer referenced by any resource that is reachable from the root or global object. Hence, these will be marked as unreachable by the garbage collector and have their allocated memory reclaimed. Some Limitations: The only limitation that can be found is that it is not possible to explicitly or programmatically trigger garbage collector in JavaScript.Hence if there are cases when it would be convenient to manually program when to release memory, there are no provisions in JavaScript to trigger such an event. My Personal Notes
arrow_drop_upSave
Cycles are no longer problem: After the function call returns, the two objects are no longer referenced by any resource that is reachable from the root or global object. Hence, these will be marked as unreachable by the garbage collector and have their allocated memory reclaimed. 
Some Limitations: The only limitation that can be found is that it is not possible to explicitly or programmatically trigger garbage collector in JavaScript.Hence if there are cases when it would be convenient to manually program when to release memory, there are no provisions in JavaScript to trigger such an event. 
javascript-basics
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript
Installation of Node.js on Linux