🌍 All
About us
Digitalization
News
Startups
Development
Design
How to Track Memory Usage and Leaks in Node.js
Startup House
Dec 10, 2021・5 min read
Table of Content
What is a 'Memory Leak' in Node.js?
What is Garbage Collection in Node.js?
Detecting Memory Leaks with Node-Inspector and Chrome Devtools
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.
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.
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.
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 hello@start-up.house


You may also
like...

Node.js: The Backbone of Modern Web Applications
Node.js revolutionizes web development by offering a JavaScript runtime for creating high-performance, scalable server-side applications. With its non-blocking, event-driven architecture and a robust npm ecosystem, Node.js is a go-to technology for developers building modern web applications.
Alexander Stasiak
Jun 04, 2024・5 min read

Mobile Backend as a Service
Mobile Backend as a Service (MBaaS) streamlines mobile app development by offering cloud-based backend functionalities such as data storage, user authentication, and push notifications. This allows developers to focus on front-end features while the service provider handles the complexities of backend management. Explore how MBaaS enhances app performance, simplifies development, and reduces costs, making it an essential tool for modern mobile apps.
Marek Majdak
Aug 09, 2024・5 min read

What is Backend as a Service (BaaS)?
Backend as a Service (BaaS) streamlines application development by handling backend functions such as database management, user authentication, and hosting. This allows developers to focus on front-end design and user experience, making it a popular choice for startups and small teams. Discover how BaaS can enhance your development process by reducing complexity and accelerating deployment.
Marek Majdak
Aug 02, 2024・5 min read