preloadedpreloadedpreloaded

How to Track Memory Usage and Leaks in Node.js

Startup House

Dec 10, 20215 min read

Node.jsBack-end development

Table of Content

  • What is a 'Memory Leak' in Node.js?

  • What is Garbage Collection in Node.js?

    • An example of an app with memory leak

  • Detecting Memory Leaks with Node-Inspector and Chrome Devtools

    • 3 Snapshots of Memory Leaks in Node.js

  • Conclusion - Know Your Tools

For the Node.js engineer, memory leaks in an application can be a nightmare from which awakening isn't always as straightforward as crying out in the night. In fact, the longer these leaks go undetected, the longer the bad dream can drag on.

This is to say that CPU, memory usage, load average and response time of an application with memory leak bugs will increase accordingly until the CPU reaches 100% of its capacity, after which the service will cease to respond. These are symptoms the developer may face when an application suffers a memory leak.

Should you be such a developer, read on and find out how to address these symptoms and how you can monitor your memory usage and memory leaks in Node.js applications.

 Arrow11.png

What is a 'Memory Leak' in Node.js?

In simple terms, a memory leak is an issue in which an application allocated a block of memory without freeing it up or where the Node.js Garbage Collector mechanism hadn't managed to sweep it.

Well, sort of...

What is Garbage Collection in Node.js?

For starters, we must bring Garbage Collector into the equation when on the topic of memory leak.  In yet more simple terms, Garbage Collector is an automatic mechanism whose role is to reclaim memory occupied by unused objects.

In the first cycle (called "mark"), the Garbage Collector marks unused objects for deletion. In the second cycle, "sweep", memory is reclaimed.

 Unicorn2.png

An example of an app with memory leak

For tutorial purposes, I have created a simple Express.js application in which each request to the application makes a leak array grow over its lifetime, thus slowing down the service. Nowhere is this leak array manually cleared, nor does garbage collection affect globally declared variables. 

Detecting Memory Leaks with Node-Inspector and Chrome Devtools

To use Node Inspector, we must run our app in inspect mode, and in CLI typescript:

node --inspect index.js

You should then get your script with the inspect option as listed in the Devtools/devices tab. Click on inspect to have Chrome Devtools attached to the Node Script. In our case, we are interested in the memory tab and heap snapshot option.

Swirl3.png

3 Snapshots of Memory Leaks in Node.js

To test if our application has a memory leak we will take 3 heap snapshots. The first one will be just after starting the server; the second will follow a benchmark of 1000 requests to our server and the third after another benchmark of 1000 requests.

The second snapshot, after the first benchmark of 1000 requests, can act as a warning sign if the heap has grown. So let’s do the last snapshot after the second benchmark: This will help confirm whether the application has a memory leak.

To find the cause, select the third snapshot; click on the comparison option and compare it to the first snapshot. This will help identify newly created objects and dates since starting the server.

The last part is to take a closer look at these objects, and then we should look for objects with the same allocation size. Upon investigating, we can locate and rectify the memory leaks in our application.

Conclusion - Know Your Tools

From here, if you stick to well-known and tested libraries, you'll find fixing any Node.js memory leak a far easier task than identifying one in the first place.  Crucial to this, of course, is having a full understanding of garbage collection in Devtools as well as any others you employ when building out your applications. With the various debuggers, leak cathers and usage graph generators at your disposal, your newly refined skills in isolating leaks where and when you find them will ensure your software performs faster and more efficiently.

At Startup Development House, we admit we're a bit expert when it comes to using Node.js and indeed solving any problems we encounter when doing so. If you're an aspiring developer and could use a bit of this expertise, then feel free to reach out to

 

Share

Published on December 10, 2021


Startup House

Digital Transformation Strategy for Siemens Finance

Cloud-based platform for Siemens Financial Services in Poland

See full Case Study
Ad image
How to Track Memory Usage and Leaks in Node.js
Don't miss a beat - subscribe to our newsletter
I agree to receive marketing communication from Startup House. Click for the details

Let’s build your next digital product — faster, safer, smarter.

Book a free consultation

Work with a team trusted by top-tier companies.

Logo 1
Logo 2
Logo 3
startup house warsaw

Startup Development House sp. z o.o.

Aleje Jerozolimskie 81

Warsaw, 02-001

 

VAT-ID: PL5213739631

KRS: 0000624654

REGON: 364787848

 

Contact Us

Our office: +48 789 011 336

New business: +48 798 874 852

hello@startup-house.com

Follow Us

instagram
facebook
Follow us on null
logologologologo

Copyright © 2026 Startup Development House sp. z o.o.