The basic of Memory Management

The basic of Memory Management

JavaScript fellows,
I am going to talk about Memory Management today. Even though we write codes with high-level languages such as JavaScript, this is rather a counter-intuitive topic as opposed to low-level languages such as C, we cannot explicitly declare how to manage the memory. It would be worth knowing how this works and what we should take into consideration for the best memory optimisation as we do care about performance!

Let's start!

All about references

clint-adair-BW0vK-FA3eg-unsplash (1).jpg

let totalNumber = 0;

function addby1(){
    return ++totalNumber;
}

addby1();
// returns 1
addby1();
// returns 2

The global variable totalNumber is assigned with a value, memory is allocated by JavaScript automatically and it takes up a space all the time. As you can see in the codes above, the global variable totalNumber keeps being incremented as we invoke the function addby1 which means it is implicitly said that the variable will be referenced by other variables or functions any time. so, please hold the value somewhere in the memory.

As a high-level language without any explicit memory management methods, this could cause detriment to the performance depending on how we write codes. If you no longer need the variable totalNumber contextually, it would be best to release it, for instance, totalNumber = null. In another case, the memory is claimed, but it's never used again; it would cause the memory leak.

When you assign null to a variable, it releases the memory for the variable as null which means that it has no value associated with it and nothing to reference.

However, as the application gets complex and bigger, it would be more difficult to determine which can be released and when they should be. We cannot explicitly manage memory with JavaScript, so we should have a good grasp of this whole mechanism in order to minimise any performance issue. However, I would dare say it is quite predictable and manageable as long as we follow the basic principles of development.

Back on the topic, memory optimisation depends on references and understanding the garbage collection algorithms.

Garbage Collection Algorithms

jilbert-ebrahimi-b0p818k8Ok8-unsplash (1).jpg

A Garbage Collection Algorithm is a way to find all the objects which still reference its global(root) objects. If not, it will be seen as garbage and collected. All the modern browsers adapt the mark-and-sweep algorithms and the implementation of other algorithms have made improvements as well. If you'd like to know how the browser engines actually work for garbage collection, check Chrome Blink out! (The Link I share with you is for Chrome browser engine called, Blink.)

I hope my article will help you to have a better understanding of Memory Management and I'll be back with another JavaScript blog next time. Thank you for reading!

Images sourced from
Photo by Clint Adair on Unsplash
Photo by Jilbert Ebrahimi on Unsplash