Garbage Collection is slowing down your code

You might already have an idea on what garbage collection is and what it does. For those who don't know, no I am not talking about Microsoft's Windows Source code. Garbage Collection is a feature built in the compiler to recover memory failure and to free up memory space that has been allocated to objects no longer needed by your program.

There are several algorithms that are available today that can be used for garbage collection. "Mark and sweep" is an example which is used by the Google Chrome's V8 engine.

So what's the deal about it? I am glad you asked.

Whenever there is garbage being collected by the compiler in the background of your program, your program is not doing anything. The only process happening at that time is garbage collection and nothing else. Although it might take only milliseconds to do the garbage collection at a particular time, but when you sum up the whole time taken by the compiler for garbage collection throughout the lifetime of your program, this can be a significant time lost(sometimes as high as 50%) where no process is happening related to your program.

So how to minimize this time lost?

Let's first go through the type of things that takes significant time in garbage collection -

  1. Creating an object via new or via literal syntax [...], or {}
  2. Concatenating strings.
  3. Entering a scope that contains function declarations.
  4. Performing an action that triggers an exception.
  5. Evaluating a function expression: (function (...) { ... }).
  6. Calling a builtin function that does any of these under the hood, like Array.prototype.slice.
  7. Splitting a string or match with a regular expression.

Avoid doing those, and pool and reuse objects where possible

class ObjectStore {
    constructor(factory) {
        this.factory = {};
        this.pool = new Array;
        this.len = 0;
    }

    get() {
        if (this.len > 0) {
            const out = this.pool[this.len - 1]
            this.len--;
            return out;
        }
        return this.factory;
    }

    releaseBulk(items) {
        for (let j = 0; j < items.length; ++this.len, ++j) {
            this.pool[this.len] = items[j];
        }
    }
}

const objectStore = new ObjectStore()

In this piece of code you can see we are initializing a pool which is the array objects. Whenever we have to create a new object we can use the get() function which will give us an object from the pool of objects and when we are done with our object we can release it back into the pool using the releaseBulk() function, so every time we need to create and object or free an object, we can use the get and releaseBulk functions, taking away the process from the garbage collection as we are reusing the objects from our own pool.

So we have seen how we can reduce time in doing garbage collection and spend more time doing the actual processing. This can save up a lot of cost for you, specially when you are leveraging a cloud service to run your code.

Try out a program on your own with and without the pool of objects and let me know in comments how much faster it made your program.

Thank you!

Did you find this article valuable?

Support Vishal Arora by becoming a sponsor. Any amount is appreciated!