My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Garbage Collector Pause

piyush jaiswal's photo
piyush jaiswal
·Sep 10, 2021·

3 min read

Garbage collection in javascript is an automated process. Automated means the developer does not have to care about the memory management. It is being taken care of by the javascript engine.

So what is Garbage collection pause?

Garbage collector pause is the situation or the time duration which a javascript engine utilizes to reclaim the memory which was assigned to an object. This happens because the javascript engine is single threaded and can perform one task at a time. And when the garbage collection process starts other processes have to stop and this is something which goes unnoticed. But the problem becomes noticeable when you do more memory intensive tasks.

For example:

const iterationNumber = 1000000;

const arr = [];

for (let i = 0; i < iterationNumber; i++) {
    arr[i] = {
        counter: 0,
    };
}
// in the below loop we are creating an array of objects of size = 1000000;
//let's try to update the array.
let t0 = performance.now();

for (let i = 0; i < iterationNumber; i++) {
    arr[i].counter = arr[i].counter + 1;
}

/* Here the reference of the object inside arr is intact 
and we are updating its value. so garbage collection will not run.*/

let t1 = performance.now();

for (let i = 0; i < arr.length; i++) {
    arr[i] = {
        counter: arr[i].counter + 1,
    };
}
/* Here we are assigning a fresh object to arr .
Hence the older object loses its reference and gets 
garbage collected */

let t2 = performance.now();
console.log("time taken without garbage collection  " + (t1 - t0) + " milliseconds.");

// "time taken without garbage collection  6.6599999845493585 milliseconds."
// this is faster because of no garbage collection process

console.log("time taken with garbage collection " + (t2 - t1) + " milliseconds.");
//"time taken with garbage collection 101.8550000153482 milliseconds.";

// this will be slower due to garbage collection. 
/* in this operation we are trying to create new objects.
 the older object {counter:0} is no longer required 
and will be garbage collected.
when we reduce the value of iterationNumber to 
100 or 10 , we will not notice anything 
but when the value of iterationNumber becomes large, 
we will start to see the difference in execution time  
and this difference is due to involvement of garbage collection 
when the object is no longer referenced.
*/

How can we avoid the garbage collector pause?

The possible solution to avoid garbage collection pause can be re-using the object. Something similar to the second loop where the arr values are getting updated instead of creating new one.

That’s it and if you found this article helpful, please give a like, you can also follow me on LinkedIn ,if you have any doubt, feel free to comment! I’d be happy to help :)

Special thanks to Aditya Mitra for suggesting this topic.