[Javascript] Memory Management And Garbage Collection In Javascript


Memory Management and garbage collection in Javascript. Slightly unfamiliar topic since in javascript we are not performing any memory operation explicitly however it is good to know how it works.

In the low-level languages like C, developers need to manually allocate and deallocate the memory using malloc(), calloc(), realloc() and free() methods. In the high-level languages like Java, Javascript we don't need to explicitly allocate or release the memory. Javascript values are allocated when things are created (objects, Strings..) and freed automatically when they are no longer used. This process is called as Garbage collection.

Memory Life Cycle:

Irrespective of any language (high-level or low-level), Memory life cycle will be almost similar as the below.
Memory Life cycle
Memory Life Cycle

In the high-level language, We will just read and write to the memory explicitly (Use Memory). In the low-level language, all the three steps developer needs to take care explicitly.

Since allocation and deallocation happen automatically, that doesn't mean developers not to care about memory management. Poor coding may lead to memory leaks, it is a condition where memory is not released even though that is no longer used by the application. So it is very important to learn more about the memory management.

Memory Allocation in Javascript:

When declaring the variable, Javascript will automatically allocate the memory for the variables.
var numberVar = 100;                // allocates memory for a number
var stringVar = 'node simplified';  // allocates memory for a string 
var objectVar = {a: 1};             // allocates memory for an object
var a = [1, null, 'abra'];          // allocates memory for the array
function f(a) {
  return a + 2;
} // allocates memory for a function 
When the memory is no longer needed anymore, then the allocated memory will be released. Memory leak and most of the memory related issue come while releasing the memory. The hardest part is finding the memory which is no longer needed and it is tracked down by the garbage collector.

Garbage collection:

It is the process of finding memory which is no longer used by the application and releasing it. To find the memory which is no longer used, few algorithms will be used by the garbage collector and in this section we will look into main garbage collection algorithms and its limitations. we will look into following algorithms.

  • Reference-counting garbage collection
  • Mark-and-sweep algorithm

Reference-counting garbage collection:

It is most important garbage collection algorithm and in which when there is no reference to an object then it will be automatically garbage collected. This algorithm considers zero referencing object as an object that is no longer used by the application.

Example:
var o = { a: { b: 2 } }; 
// 2 objects created. One is referenced by the other as one of its properties.
// Obviously, none can be garbage-collected

o = 1;     // what was the 'a' property of the object originally in o 
           // has zero references to it. It can be garbage collected.

Limitation: cycles

function func() {
  var o = {};
  var o2 = {};
  o.a = o2; // o references o2
  o2.a = o; // o2 references o
  return 'true';
}

func();
Consider the above code snippet in which o is referenced to o2 and o2 is referenced to o and it creates a cycle. When the scope goes out of the method, then these two objects are useless however garbage collector unable to free the memory since those two still got the reference to each other. It leads to memory leaks in the application.

Mark-and-sweep algorithm:

The garbage collector uses this algorithm will free the memory when an object is unreachable rather than zero referencing object.

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

This algorithm is far superior to the above algorithm and object which doesn't have reference will be unreachable objects too so zero reference objects will be garbage collected and cycle limitation will be solved by this algorithm.


If you enjoyed this article, Please share it with your developer friends and in social media. 
Follow our facebook page for latest updates.

Comments

  1. Hello Raghu,

    Nice blog! I am editor at Web Code Geeks (www.webcodegeeks.com). We have the WCG program (see www.webcodegeeks.com/join-us/wcg/), that I think you’d be perfect for.

    If you’re interested, send me an email to eleftheria.drosopoulou@webcodegeeks.com and we can discuss further.

    Best regards,
    Drosopoulou Eleftheria

    ReplyDelete
  2. Nice Article, for javascript interview questions you can visit https://www.onlineinterviewquestions.com/javascript-interview-questions/

    ReplyDelete

Post a Comment