How to Troubleshoot Heap Limit Under-Provisioning in Node.js

Production issues in Node.js applications can be challenging to diagnose, especially when different problems exhibit similar symptoms. To make troubleshooting easier, we’re introducing a Node.js Production Troubleshooting Series, where each article focuses on one specific production issue, explores its underlying causes, and demonstrates a practical, step-by-step approach to identifying the root cause.

Heap Limit Under-Provisioning is one of those production issues that looks fine on a host memory gauge and still hurts the app. The machine may have plenty of RAM, but Node.js was started with a small `–max-old-space-size`. Once the real working set fills that V8 budget, garbage collection gets aggressive, latency climbs, and if nothing changes, you eventually hit FATAL ERROR: Reached heap limit.

In this blog, we’ll walk through how Heap Limit Under-Provisioning shows up in a Node.js application, what typically causes it, and how to trace it back to the exact root cause. We’ll also simulate the issue in a sample application, capture diagnostic data as it happens, and analyze it step by step to pinpoint what’s going wrong.

What is ‘Heap Limit Under-Provisioning’ in Node.js?

Node.js does not use all of the machine’s RAM for JavaScript objects. V8 gives the process a heap limit, usually controlled by `–max-old-space-size`. That limit is the ceiling for JavaScript heap usage. When your retained objects climb toward that ceiling, V8 spends more time in garbage collection, and the process can freeze or crash even though App Memory on the host still looks low.

Under-provisioning means the working set is larger than the heap you gave Node.js. That is different from a classic leak story where unused objects keep growing forever. You can have a legitimate cache or dataset that simply does not fit the limit.

What Causes ‘Heap Limit Under-Provisioning’ in Node.js?

Here is the usual list of causes for the ‘Heap Limit Under-Provisioning’ problem.

  1. Small –max-old-space-size for a real workload: A 256 MB or 512 MB limit that was fine in staging, then production caches, payloads, or in-memory indexes outgrow it.
  2. Large retained working sets: Caches, session maps, or report buffers that are meant to stay in memory and simply need more heap headroom.
  3. Growth without an eviction policy: In-memory structures that keep growing under traffic until they press against the V8 limit.
  4. Confusing host RAM with V8 heap: Watching App Memory or RSS only, while heap used versus `heap.memoryLimit` is already in the danger zone.
  5. Copying defaults across hosts: Reusing the same Node flags on a larger machine without resizing the heap for the actual working set.

How to Simulate Heap Limit Under-Provisioning in Node.js

To understand how Heap Limit Under-Provisioning appears in the diagnostic data, let’s reproduce the issue using a sample Node.js application.

The story here is not “we forgot to free memory.” It is “the working set is larger than the heap we gave Node.js.” The following program keeps allocating unique JavaScript objects on the V8 heap and holds onto them. It does not use `Buffer.alloc`, because large Buffers live outside the V8 heap and would look like a Buffer Leak instead. The process fills until used memory sits around 85% of `heap_size_limit`, which is the band where yCrash warns hard that you are running out of headroom.

``` JS Code (heap-limit.js)
const v8 = require('v8');
function uniqueHeapChunk(id, targetBytes) {
const approxBytesPerObject = 120;
const count = Math.max(1, Math.floor(targetBytes / approxBytesPerObject));
const chunk = new Array(count);
for (let i = 0; i < count; i++) {
chunk[i] = { t: id, i: i, v: (id * 131 + i) >>> 0 };
}
return chunk;
}
class HeapLimitDemo {
constructor() {
this.isRunning = false;
this.intervalId = null;
this.allocationCount = 0;
this.chunks = [];
}
start() {
if (this.isRunning) return;
this.isRunning = true;
this.chunks = [];
console.log('Starting heap limit under-provisioned simulation...');
this.intervalId = setInterval(() => {
const limit = v8.getHeapStatistics().heap_size_limit;
const heapUsed = process.memoryUsage().heapUsed;
// Hold around 85% of the V8 heap limit so the process stays up for capture.
if (heapUsed >= limit * 0.85) {
console.log('Holding heap at 85% of limit — capture now');
return;
}
for (let i = 0; i < 4; i++) {
this.chunks.push(uniqueHeapChunk(this.allocationCount, 2 * 1024 * 1024));
this.allocationCount++;
}
}, 500);
}
stop() {
clearInterval(this.intervalId);
this.chunks = [];
this.isRunning = false;
}
}
module.exports = new HeapLimitDemo();
```

How to Capture Diagnostic Data for Troubleshooting Heap Limit Under-Provisioning

yCrash is a diagnostic tool that captures runtime performance data from a live Node.js process, including CPU usage, event loop lag, worker thread activity, and call stacks, and turns that data into a report that points to the root cause. To set up yCrash for your Node.js application, follow the steps below:

1. Install yCrash

yCrash is available in both Cloud and On-premises versions. Use one of the options below to get started:

  • Cloud Service: Create an account to use the cloud version.
  • On-premises: Register for a 14-day trial and install the yCrash application on your local machine or within the organization’s environment.

2. Set up the yCrash Node.js hook first

Please note that this is a one-time setup. The hook must be loaded into your Node.js application before yCrash can capture the Node.js internal data that matters for diagnosis, things like worker thread CPU, event loop lag, method profiling, and call stacks. To set this up, follow the Hook Mode steps here: Node.js Diagnostic Capture. This hook setup adds very minimalistic, almost zero overhead to your application.

3. Launch yc-360 script

Configure and start the yc-360 script to monitor your Node.js process. The full setup and configuration options specific to your deployment are available here: Micro-metrics Monitoring (M3) Mode

When the yc-360 script detects a problem, it captures  360° diagnostic artifacts (GC log, Event loop lags, Worker threads, CPU Profile, Unhandled rejections, Call stack, Active handles, Process, Storage, Kernel, Network, etc.). It uploads them to the yCrash server automatically. yCrash server analyzes the artifacts using advanced pattern recognition and ML algorithms to identify the root cause of the problem.

How to Analyze Heap Limit Under-Provisioning Using the Diagnostic Data

In this section, we’ll see how to analyze Heap Limit Under-Provisioning using the diagnostic data captured by yCrash. We’ll start with the incident on the calendar dashboard, check the RCA Summary to understand what’s going on, and then drill into Process Overview, Heap Space, and Event Loop Lag to confirm the process is out of V8 headroom.

Step 1: Open the incident from the yCrash dashboard

When the yc-360 script detects any performance degradation in the Node.js application, it automatically captures 360° diagnostic data and transmits it to the yCrash server. The yCrash server analyses this data and creates an incident in the yCrash dashboard as shown in the screenshot below. Open that incident from the yCrash calendar dashboard to view the reports.

Fig: yCrash dashboard, incident list view

Step 2: Start with the RCA Summary Page

Before diving into details, let’s start with the RCA Summary. yCrash analyzes the Node.js diagnostic data and brings the key findings together in simple language with their severity levels.

In the RCA Summary, we will focus on two sections:

  • AI Overview
  • Issues in application and device

AI Overview

At the top of the RCA summary report page, you’ll see an AI Overview section. It gives you an AI-generated summary of the incident, which would make even a non-technical person understand the root cause of the problem. Within the AI Overview section, below the AI summary, you can also click on the ‘Dive Deeper in AI Mode’ button to open a chat window to ask any question to the AI about this incident.

Fig: RCA Summary highlighting the AI Overview

As shown in the AI Overview above, the main problem is high heap usage at 85.6% of a 560 MB limit. The overview also mentions an event listener increase and a short event-loop stall. Those are secondary here. The heap used versus the V8 limit is what this incident is about.

Issues in application and device

Fig: RCA Summary highlighting the issues in application and device

As shown in the above screenshot, several application-level issues are reported. The finding that matters most for Heap Limit Under-Provisioning is:

  • High heap usage

You will also see an event listener leak warning, a duplicate `ms` package warning, and a short event-loop block of 302 ms. Treat those as secondary. The listener and package findings are not what this incident is about. The event-loop stall is often collateral when the process is allocating hard or GC is working harder near the limit.

Step 3: Confirm heap pressure in Process Overview

Open the Node.js Internals report from the left navigation. Start with Process Overview and open the Resource Usage & Limits tab.

Fig: Process Overview showing heap used versus heap memory limit

In this capture, the banner says the JavaScript heap is 86% full: 479.92 MB used of a 560.00 MB limit. The table shows the same story in numbers: heap.usedMemory at 479.92 MB, heap.memoryLimit at 560.00 MB, and heap.old_space.memorySize around 417.93 MB. That is under-provisioning in the open. The process is running out of V8 headroom, not host RAM.

Step 4: Confirm Heap Space and rule out a Buffer Leak

Open Heap Space from the left navigation.

Fig: Heap Space Report showing heap at 85.6% of the 560 MB limit

Heap Space repeats the pressure finding at 85.6% of the 560 MB limit. Old space is heavily used (about 410.48 MB used of 417.68 MB allocated, with only about 106 KB left). The used-versus-available chart is almost entirely filled.

Step 5: Check Event Loop Lag as a secondary signal

Open Event Loop Lag from Node.js Internals. Near the heap limit, allocation and GC work can briefly stall the single thread.

Fig: Event Loop Lag showing a 302 ms spike

In this capture, the lag spiked to 302 ms at 14:10:09, then settled back toward the teens of milliseconds. That spike is useful context, not the primary diagnosis. The primary question was already answered by Process Overview and Heap Space: the JavaScript heap was about 86% of a 560 MB limit.

How to Fix Heap Limit Under-Provisioning in Node.js

The following are the potential solutions for this issue:

  1. Confirm whether the retained memory is a legitimate working set or a leak. Use –inspect and Chrome DevTools Allocation Timeline or a heap snapshot when you need the retaining path.
  2. If the working set is legitimate, raise –max-old-space-size so the process has enough headroom for peak load. Size it from measured heap used plus a safety margin, not from a round number someone picked once.
  3. If the memory is not legitimate, bound or evict in-memory caches, queues, and object graphs so they cannot grow without a limit.
  4. Watch heap used versus heap limit in production, not only RSS or the App Memory gauge. Under-provisioning hides on those gauges until V8 is already thrashing.

Conclusion

Diagnosing and identifying the root cause of Heap Limit Under-Provisioning can be challenging when host memory gauges still look fine. In this example, the diagnostic data was captured using the yc-360 script and analyzed using the yCrash server, which helped correlate Process Overview and Heap Space with a V8 heap sitting around 85-86% of a 560 MB limit.

The analysis showed `heap.usedMemory` at 479.92 MB against `heap.memoryLimit` at 560.00 MB, with old space holding most of the retained data. A short 302 ms event-loop spike sat next to that pressure as collateral, not as the main finding.

Either give the process a heap large enough for the real working set, or make sure that working set cannot grow past a clear upper bound. That is what prevents the under-provisioning pattern from coming back.

Mahesh Devda
Mahesh Devda
Articles: 21

Share your Thoughts!