JavaScript

Memory Management – Garbage collection in JavaScript

JavaScript doesn’t provide much in the way of memory management or garbage collection because we cannot directly use operations related to memory but for knowledge purposes, it is good to know how it operates.

In C language, developers manually allocate or deallocate memory with malloc(), calloc(), realloc() and free() methods.

JavaScript values work as the values are allocated after the creation of objects, or strings; and are automatically freed when the process is completed, so this whole process is known as Garbage Collection. The Memory Management Lifecycle is based on 3 steps.

  1. Allocation of Memory
  2. Utilization of Memory
  3. Releasing Memory

Memory allocation in JavaScript

After introducing the variable, JavaScript allocates memory for its assigned variables. At the point when the memory is not useful any longer, then memory will be released. When memory is released then several issues including leakage of memory arise. The most difficult task is to discover the memory which is not useful and subsequently deallocate the memory efficiently with the help of a garbage collector. The garbage collector tracks the memory which is not required, but the most difficult task is to track unused memory.

If you declare values to variables then memory allocation in JavaScript is assigned automatically.

In these examples, variable1 allocated memory for a number whereas the string is allocated in the memory of variable2 as shown below.

var variable1 = 100;
var variable2 = "Memory Allocation";

If you want to assign memory for numerous objects then use the following code.

var variable3 = {

variable4: 5,

variable5: 'Test'

}

Array allocated in the memory of variable6 as shown below.

var variable6 = [1,2,3,4,5]

You can also allocate memory for distinct methods. Here is the code to allocate memory for a method.

function function_name(x) {

return x + 20;

}

Garbage collector

The process of memory allocating and releasing the memory when not required is known as garbage collection. Algorithms are used to find out which memory is irrelevant for the tasks. This section explicitly deals with the main algorithms used in garbage collection and their limitations.

Reference counting garbage collection algorithm

Reference counting garbage collection algorithms are viewed as the great fundamental sort of garbage collection. This algorithm not only decides if any resource is significant or not, it also filters the memory to decide whether an item has some other instances referring to it. An instance that has null references is accounted for as garbage.

Mark & sweep algorithm

A garbage collector helps to free up memory whenever an instance is inaccessible, instead of a zero referring to an object. Initially, the garbage collector searches all global or root instances along with their object references. Through this algorithm, the collector will distinguish between the reachable and inaccessible instances. Subsequently, inaccessible instances will be collected automatically by the garbage collector.

Conclusion

JavaScript efficiently allocates memory and subsequently, the garbage collector helps to detect this allocated memory and reclaim it whenever memory is not useful. We learned how we can manage memory and collect garbage in JavaScript in this article. We need to take care that we have to implement software programs that cannot cause circular referring to variables. We have to make sure that we cannot program anything which will throw variables out of the garbage cycle.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.