DEV Community

Cover image for Garbage Collection in Javascript
Abhilash
Abhilash

Posted on

Garbage Collection in Javascript

We shall today discuss memory management and garbage collection in JavaScript.Even though in JavaScript we are not performing any memory operations explicitly, however, it is good to know how it works.

In the low-level languages like C, programmers need to manually allocate and deallocate the memory using the malloc(), calloc(), realloc(), and free() methods.

In high-level languages like Java and JavaScript, programmers don't need to explicitly allocate or release memory.JavaScript memory is allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

  var a = 1;  // allocates memory for a number
  var b = {a: 1}; // allocates memory for an object
Enter fullscreen mode Exit fullscreen mode

When the allocated memory is no longer needed, then it will be released.
Memory leaks, and most memory-related issues, occur while releasing memory.

Garbage Collection:

Garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage or memory occupied by objects that are no longer in use by the program.
We shall look into two algorithms used for garbage collection:-

1). Reference-counting garbage collection.

Reference Counting is a programming technique of storing the number of references, pointers or handles to a resource, such as an object, a block of memory, disk space, and others.

In garbage collection algorithms, reference counts may be used to deallocate objects which are no longer needed. If in reference counting algorithms, there are no references to an object, it will be automatically garbage collected.

This algorithm considers zero referencing object as an object that is no longer used by the application.

  var object1 = { object2: { value: 2 } };
  object1 = 2; // object2 now has zero reference count, hence memory will be released
Enter fullscreen mode Exit fullscreen mode

However, this algorithm has limitations with cycles.

consider the below function,

  function cycleReference() {
    var object1 = {};
    var object2 = {};
    object1.a = object2; // object1 references object2
    object2.a = object1; // object2 references object1
    return 'done';
  }
Enter fullscreen mode Exit fullscreen mode

In the above function in which object1 is referenced to object2 and object2 is referenced to object1 and it creates a cycle.
When the scope goes out of the function, then these two objects are useless. However, the garbage collector is unable to free the memory since those two still got the reference to each other.
It leads to memory leaks in the application.

2). Mark and Sweep algorithm.
The garbage collector uses this algorithm to free memory when an object is unreachable, rather than a zero referencing object.

The garbage collector will first find all the global objects or root objects and will find all the references to these global objects and references to the reference object, and so on. Using this algorithm, the garbage collector will identify the reachable and unreachable objects.
All the unreachable objects will be automatically garbage collected.

Let us try to implement something similar to this algorithm.

Top comments (0)