Memory Management and Memory Leaks in Node.js

Understand Node.js Memory Management with Short Questions and Answers and Examples.

4 min read

November 19th, 2024

Nick Mousavi

Memory Management

This refers to how a software application interacts with computer memory, specifically focusing on

allocation
and
deallocation
.

Garbage Collection in Node.js

Is actually an automatic process managed by the

that reclaims memory no longer used by the application. Node.js utilizes the
mark-sweep
algorithm.

Memory Leaks:

It occurs when an application continuously allocates memory without releasing it after it is no longer needed. This can lead to performance degradation and eventually application crashes.

In official

doc, we have a great example also I'm gonna share a few examples that can lead to memory leaks:

1. Unbounded Caching And Closures

  • the problem is adding items to cache without limits
  • Actually storing any large objects, except using
    WeakMap
    and
    WeakSet
    .
  • we can address this issue by setting limit to cache size like using
    LRU
    (Last Recently Used) strategy to remove oldest cached data.
const cache = new Map();

function badCaching() {
  // ❌ BAD: Cache grows infinitely
  setInterval(() => {
    const key = Date.now();
    cache.set(key, { data: 'my data'});
  }, 100);
}

function goodCaching() {
  // ✅ GOOD: Set a max size
  const MAX_CACHE_SIZE = 100;
  if (cache.size >= MAX_CACHE_SIZE) {
    const oldestKey = cache.keys().next().value;
    cache.delete(oldestKey);
  }
}

2. Event Listeners Not Being Removed

  • this one is familiar to web developers, when you add listener fo an event, consider to remove your listener when you finished or your component is unmounted.

Most common used methods are

and
cancelAnimationFrame
that I used recently.

3. Long-Lived Timers

const interval = setInterval(() => {
    console.log("I run forever!");
}, 1000);
// Forget to clear it when not needed.
clearInterval(interval);

Short Answer Questions about Memory Management and Garbage Collection

The

V8 engine
powers Node.js and is responsible for managing the heap memory where objects and functions are stored. It automatically
allocates
and
deallocates
memory, including executing the garbage collection process.

The "mark-sweep" algorithm works in two phases. First, it "marks" all reachable objects starting from the root. Then, it "sweeps" through the memory, reclaiming the space occupied by unmarked (unreachable) objects.

process.memoryUsage()
function be helpful in monitoring Node.js application memory?

The process.memoryUsage() function provides a snapshot of the memory usage of the current Node.js process. It returns key metrics like resident set size, total heap size, used heap size, and external memory usage, allowing developers to assess memory consumption.

Global variables persist for the entire lifetime of a Node.js application. If they reference large objects or accumulate data over time without being cleared, they prevent the garbage collector from reclaiming the memory, leading to memory leaks.

WeakMap and WeakSet are collections that hold weak references to objects. This means that if the object being referenced is no longer reachable elsewhere in the application, the garbage collector can reclaim its memory even if it's still present in the WeakMap or WeakSet. This helps prevent memory leaks.

Instead of loading or buffering the entire file, we can stream data to process it in smaller chunks.

Tags:NodeJS