Open In App

Garbage Collection in JavaScript

Last Updated : 29 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. 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 object
    var 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 object
    var object_4 = object_1;
      
    // The object that was originally in 
    // "object_1" has a unique reference 
    // embodied by the "object_4" variable
    object_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 references

    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();

    
    

  2. 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.
     

  3. 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.
     



    Like Article
    Suggest improvement
    Share your thoughts in the comments

Similar Reads