JavaScript Memory Management System

Kamran Namazov
Webtips
Published in
3 min readAug 1, 2020

The main goal of this article is to help to readers to understand how memory management system performs in JavaScript.

I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

The JavaScript Engine’s Garbage collector’ looks out for objects which are unreachable and also removed from the memory.

So I will share an example below and show you steps that will be needed to work with GC.

var number = 50;                // allocates memory for a numbervar string = 'textual data';  // allocates memory for a stringvar object = {x: 10};         // allocates memory for an objectvar x= [10, null, 'abra'];     // allocates memory for the arrayfunction f(x) {               // allocates memory for a function
return x * 2;
}

The example shows how JavaScript allocates memory for the variables when they declare. But when the memory is no longer needed, the allocated memory will be released. GC finds the memory no longer used by the application and releases it. But the main question is that how GC finds the memory no longer used?

The basic algorithm for doing that is called “mark-and-sweep”.

In the mark-and-sweep algorithm, the first step is to take root and remember them by GC, later remembers all references from them. Then visits marked objects and remember their references. Yes, it is done. All visited objects are remembered, all objects except unreachable ones are removed by GC.

var myObj = { a : 10 }

Now a is reachable from the root. But if I set the value nullfor this variable the object will not be reachable and will be garbage collected.

Another algorithm is called “reference-counting garbage collection”.

How it works? If there is no reference attached to an object in reference counting algorithms, so it will be automatically garbage collected.

Let’s take a look an example:

function foo() { var a = { 
myproperty: {
y: 10
}
};
var b = a.myproperty; b.myproperty = a; return 'textual data'
}
foo();

If you look through the code line by line it will be clear that a has an object in which its myproperty holds one object. As the a has the reference to the object, no need for garbage collection. But later happens issue). Another variable b also has the reference to the same object that was referred by a ,because we have written var b = a.myproperty .But later a was updated with "textual data" .

Here the reference counting algorithm will not remove a and b from the memory after the function call, since both the objects are referenced by each other. So b has been unreferenced by updating "textudal data". Therefore it might seem that the object it held before has no references to it and can be garbage collected. But the idea would be wrong if we say as a.mypropertyhas the reference of b.myproperty. Therefore it won’t be garbage collected.

Thanks for reading.

--

--