Measuring used JS heap size in nodejs

In the former article, we saw how to measure the JS heap in the browser and a few use cases it can come in handy. I was asked how this can be done in a nodejs app as well.

Let’s do something a bit more interesting – create a script that will measure the heap size both in the browser as well as in a nodejs environment.

Let’s take the example from the former article and turn it into a nodejs compatible code:

function logMemory() {
if (typeof process != 'undefined') {
console.log(`Node: ${process.memoryUsage().heapUsed / Math.pow(1000, 2)} MB`);
} else if (performance) {
console.log(`Browser: ${performance.memory.usedJSHeapSize / Math.pow(1000, 2)} MB`);
} else {
throw ('Where d-heck are you trying to run me?');
}
}
function measureMemory() {
const arraySize = 25 * Math.pow(1000, 2);
logMemory();
(function() {
const array1 = new Array(arraySize).fill(1.1);
logMemory();
})();
(function() {
const array2 = new Array(arraySize).fill(1);
logMemory()
})();
setTimeout(() => {
logMemory();
}, 5000);
}
measureMemory();
view raw logMemory.js hosted with ❤ by GitHub

Code snippet 1: the function log memory now logs memory both in the browser as well as in a nodejs application.

Code snippet 1 is almost the same as the code in the former article with 2 differences:

  1. The logMemory function was added
  2. The logging before and after array creation is replaced by logMemory

Log memory asks if we have the process object (e.g. the nodejs process) and if it does, it uses process.memoryUsage().heapUsed . This is the equivalent of performance.memory.usedJSHeapSize in the browser.

Running node logMemory will avail an output that looks like this:

Figure 1: logMemory output in a nodeJS environment

Running the code in the browser will output:

Figure 2: logMemory output in the browser.

You can try the plnkr here:

Code snippet 2: The logMemory code in the browser

Comparing Figure 1 to Figure 2, you can see a difference in how memory is managed between nodejs and the browser. This very well might depend on the browser, the nodejs/browser version and more.

Summary

Measuring the used heap size in nodejs is done using:

process.memoryUsage().heapUsed

Measuring it in the browser is done using:

performance.memory.usedJSHeapSize

We’ve created a short script that logs the memory regardless of the environment it is in:

function logMemory() {
    if (typeof process != 'undefined') {
        console.log(`Node: ${process.memoryUsage().heapUsed / Math.pow(1000, 2)} MB`);
    } else if (performance) {
        console.log(`Browser: ${performance.memory.usedJSHeapSize / Math.pow(1000, 2)} MB`);
    } else {
        throw ('Where d-heck are you trying to run me?');
    }
}

We also saw that memory management differs in nodejs and the browser with the small prints that it might also differ between nodejs and browser versions.

Sign up to my newsletter to enjoy more content:

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments