Arezgitfield notes / engineering
Developer toolsUPDATED NOV 23, 2026

Diagnose Node.js Retention Before Raising Memory Limits

Separate RSS from JavaScript heap growth, compare equivalent lifecycles, and trace retained ownership before collecting a heap snapshot or changing limits.

Arezgit Editorial5 min read
AREZGIT / FIELD NOTEDEVELOPER TOOLS
A rising process memory graph does not identify which owner is retaining data. Distinguish managed heap, external allocations, and resident memory, then compare equivalent lifecycle points.
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A rising process memory graph does not identify which owner is retaining data. Distinguish managed heap, external allocations, and resident memory, then compare equivalent lifecycle points. Raising a memory limit before identifying the category can postpone the failure while leaving an unbounded owner unchanged.

This guide uses Node.js 24 and a synthetic event-listener lifecycle. The executable example verifies listener ownership and cleanup, not a byte-level leak measurement. Heap collection is discussed as an investigation step for a disposable process; no real application snapshot or production memory benchmark is claimed.

Separate the memory categories before interpreting growth

The Node.js 24 process memory API reports RSS, managed heap fields, external memory, and array-buffer allocations. These categories are not independent buckets to add together: array-buffer memory is included within external memory. RSS describes resident process memory beyond just live JavaScript objects.

A synthetic trace with stable post-workload heap usage and rising RSS suggests a different investigation from one with growing reachable JavaScript objects. Native allocations, buffers, allocator behavior, and workload changes remain possibilities. Do not label either trace a confirmed JavaScript leak based on one aggregate graph.

Record workload, concurrency, runtime version, sampling interval, and lifecycle stage. Compare a warmed process after the same work and cleanup rather than a cold startup against peak load. Garbage collection timing and transient allocations can make adjacent samples look different even when the long-lived ownership set is unchanged.

For buffers, inspect the owner and allocation path instead of relying only on object count. A small JavaScript wrapper can keep a larger allocation reachable. Conversely, a large allocation rate can create pressure without long-lived retention. Allocation and retention answer different questions.

Make the lifetime contract observable

Consider a synthetic subscription that captures a payload until its listener is removed. Run this JavaScript module in a clean Node.js process:

import assert from "node:assert/strict";
import { EventEmitter } from "node:events";

const bus = new EventEmitter();
function subscribe(payload) {
  const listener = () => payload.length;
  bus.on("tick", listener);
  return () => bus.off("tick", listener);
}

const stop = subscribe(new Array(1000).fill("synthetic"));
assert.equal(bus.listenerCount("tick"), 1);
stop();
stop();
assert.equal(bus.listenerCount("tick"), 0);

The accepted lifecycle has one listener while subscribed and none after repeated cleanup. Removing the listener breaks the bus's ownership path to that closure. This assertion does not prove when the engine reclaims the payload, and the local stop variable itself still references the cleanup closure until its scope ends.

That last detail matters when reading a heap graph: removing one owner is insufficient if another long-lived collection retains the same closure. Inspect all relevant retaining paths. Do not declare a memory fix complete because a visible subscription disappeared from one registry.

Add equivalent cleanup assertions for failure, cancellation, and partial initialization in the real component. A success-only test can miss a listener installed before a later setup step throws. Bind cleanup ownership to the same task or object that defines the payload's intended lifetime.

Collect deeper evidence from an expendable process

Node's heap snapshot guidance warns that collection stops main-thread work and can require enough additional memory to crash the process. The V8 API documentation also describes snapshot generation as synchronous. Treat collection as an operational action with an availability budget.

Reproduce with synthetic data on a disposable instance first. Drain or isolate an instance before collecting from a real service when the deployment design permits that approach. Do not expose an unauthenticated diagnostic endpoint or a broadly reachable inspector to make collection convenient.

Snapshots can contain application values, so treat them as sensitive diagnostic artifacts. Restrict access, storage, and retention. An investigation should not copy a production snapshot into an ordinary issue attachment or public repository. If data cannot be handled through an approved path, use less revealing telemetry and narrow the reproduction instead.

Compare snapshots or allocation evidence at equivalent stages and look for an unexpected long-lived owner. In the listener scenario, the useful hypothesis is a bus retaining listeners after task completion. Test that hypothesis by fixing lifecycle cleanup and checking that the owner count stops accumulating under repeated completed tasks.

Fix the owner and verify the bounded behavior

For a cache, define entry count, byte budget, and eviction behavior. For listeners and timers, define installation and disposal ownership. For in-flight operations, define cancellation and completion removal. A container with an intentional lifetime can still become unbounded when its removal path is incomplete.

Change one retention mechanism at a time and replay the representative lifecycle. Observe object or owner counts alongside the relevant memory category. If those counts stabilize but RSS remains high, continue investigating that category rather than assuming the cleanup failed or claiming a universal improvement from one run.

Increasing a memory limit can be a temporary capacity decision when justified by the workload and host budget. Record that as mitigation, with a trigger and a follow-up investigation, rather than describing it as removal of the retention cause. Bounded work queues address a related owner: pending tasks that retain their inputs while admission exceeds service capacity.

End the investigation with a reproducible ownership statement: which object kept which data reachable beyond its intended lifetime, which transition now releases it, and what observation verifies that repeated work remains bounded. That statement is more actionable than a larger heap limit or a screenshot of a temporarily lower memory graph.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit