· By John Kavanagh

Node.js Memory Growth When the V8 Heap Looks Fine

Photograph of wooden blocks arranged in rising steps on a pale surface.
Image by Volodymyr Hryshchenko, licensed from Unsplash.

A Node process can use considerably more memory whilst its JavaScript heap chart barely moves. That makes a reassuringly flat chart rather less reassuring when the process keeps approaching its memory limit.

Before looking for a missing cleanup call, I want to know which memory measurement is growing, what the application is doing at that point, and whether the growth survives a quieter period. A JavaScript object retained in a cache, a large Buffer retained by that object, and repeated temporary allocations can produce quite different evidence.

This example uses Node 20.13.1 and three deliberately small workloads. Each runs in its own child process so that one experiment doesn't inherit the previous experiment's allocator state. My article on testing the real Node process covers the same reason for keeping process boundaries in a test.


Start with All Five Measurements

The Node process API returns five memory fields, in bytes:

  • heapUsed is the part of V8's heap currently in use.
  • heapTotal is the memory allocated for V8's heap.
  • external accounts for C++ objects associated with JavaScript objects managed by V8.
  • arrayBuffers includes ArrayBuffer storage and Node Buffers. It is already included in external.
  • rss is the process's resident memory, including JavaScript, native allocations, and code.

These aren't five independent buckets. Adding them together produces an impressive number, but not a useful total.

Keep the raw bytes and attach a stage label instead of logging an unexplained number:

import { performance } from 'node:perf_hooks';
import { threadId } from 'node:worker_threads';

function sample(stage, step) {
  return {
    stage,
    step,
    elapsedMs: performance.now(),
    units: 'bytes',
    pid: process.pid,
    threadId,
    ...process.memoryUsage(),
  };
}

console.log(JSON.stringify(sample('before-work', 0)));

I keep the Node and V8 versions, OS, startup flags, and workload sizes alongside the samples. A graph without that context is difficult to compare with the next run, particularly after changing the runtime.


Give Each Workload a Different Ownership Pattern

The first workload retains ordinary JavaScript records in an array. Each step adds another batch of records with strings and small arrays. Nothing removes them until the final release step. This gives us an intentionally growing set of reachable JavaScript objects.

The second retains Buffers. There is still a JavaScript object for each Buffer, but its backing storage is accounted for outside the ordinary JavaScript heap. A small‑looking collection of wrappers can therefore keep a substantial payload alive.

const retained = [];
const bytesPerStep = 8 * 1024 * 1024;

for (let step = 1; step <= 6; step += 1) {
  retained.push(Buffer.alloc(bytesPerStep, step));
}

console.log({ retainedPayloadBytes: retained.length * bytesPerStep });
retained.length = 0;

Filling the Buffer matters here. The experiment touches its contents rather than assuming that an untouched allocation becomes resident identically on every operating system. Six steps retain 48 MiB of payload, irrespective of the additional overhead reported by the process.

The third workload repeatedly allocates and drops filled Buffers. It records cumulative allocation separately from retained payload. Allocating 192 MiB over several steps does not mean 192 MiB remains reachable at the end. Confusing those two quantities makes ordinary allocation work look like permanent retention.

These small workloads make the ownership differences easier to inspect.


Compare Stages, Not Isolated Peaks

For each child, take a baseline, sample immediately after each allocation step, and sample again after a controlled collection. Finally, remove the retained references and sample after another collection. Repeat the whole sequence in fresh processes.

The lab uses --expose-gc to make that collection control explicit. It isn't a recommendation to add global.gc() to a request handler. Nor does removing a reference promise that every process‑memory measurement will immediately return to its starting value.

Look at the series together. Rising heapUsed after collection supports investigating reachable JavaScript objects. Rising Buffer accounting with comparatively little heap movement directs attention towards payload ownership. Repeated allocation with a bounded live set asks a different question again: what happens to resident memory whilst the runtime and allocator reuse storage?

RSS can stay elevated after the application releases its references. That observation alone doesn't establish a leak, fragmentation, or a particular allocator bug. It tells us to keep investigating with the other measurements and the actual workload, rather than announcing a root cause from the shape of one line.

The checks should verify sample labels, units, ordering, and deliberate retention counts. I wouldn't make a test fail because a collection reclaimed a different number of bytes on another machine.


Keep Process and Worker Scope Straight

A worker changes the interpretation. Node documents rss as process‑wide, whilst the other fields describe the calling thread. Two workers reporting RSS are reporting the same shared process at different instants; adding their figures double‑counts it.

Record the process ID and thread ID with each sample. If the heap looks quiet in the main thread, inspect the workers doing the allocation before deciding that V8 isn't involved. A snapshot of the main thread also won't contain another worker's heap.

A separate child process has its own memory accounting. If a build launches several children, inspect their contribution as well as the parent, and use the hosting platform's actual memory accounting when comparing against a container limit.

Likewise, --max-old-space-size constrains V8's old generation. It does not cap Buffer storage or total process RSS. Increasing it won't repair retained external payloads and may leave less headroom for everything else.


Take a Snapshot Only When It Answers the Question

When the evidence points towards reachable JavaScript objects, a heap snapshot can help trace what retains them. I start with a disposable reproduction containing synthetic data, with enough spare memory to survive the capture.

Node's snapshot documentation warns that creation blocks the event loop and can require roughly twice the current heap size. Taking one in a process already pressed against its memory limit can finish the job for it.

That pause is real work blocking the thread, as I explained in my event‑loop guide. A snapshot is not a harmless background photograph. It may also contain application data and secrets, so its storage and retention need the same care as other sensitive diagnostic material.

For external‑memory growth, a snapshot may reveal JavaScript wrappers keeping payloads reachable. It won't, by itself, explain every native allocation or every byte of RSS. Choose it because the retention question needs it, not simply because it is the largest diagnostic file available.


Wrapping Up

I want the next investigation to follow the evidence: retained JavaScript objects, retained external payloads, worker allocation, or process memory that stays high after a bounded workload. Those lead to different changes.

Keep the workload small, label the stages, and preserve the measurements before adjusting limits. A quieter heap chart is useful information. It just isn't an acquittal for the rest of the process.

Postscript

November 2026: I've added my memory‑diagnosis lab with the three controlled workloads, retained observations, a worker‑scope check, and an optional snapshot of a disposable process containing only synthetic records. I tested the current companion on Node 22.23.5 and 24.14.0, plus the historical Node 20.13.1 runtime. The measurements were made in November 2026; they aren't recovered results from 2024. RSS recovery varies, so the checks verify the experiment's controls without promising an identical memory curve.

Downloadable Resource

  • Node.js Memory Diagnosis Lab

    Compare JavaScript retention, Buffer retention, and allocation churn with labelled memory measurements, worker‑scope checks, and a disposable synthetic snapshot.

    Download ZIP

Looking for technical direction?

I support teams that need senior judgement on React, Next.js, headless CMS architecture, performance, migrations, and technical SEO.