Course contentsShow
← Node.js
Lesson 1893 of 1,98017. Performance Tuning and ProfilingFree lesson

Comparing V8 Heap vs RSS Usage

You will distinguish between language heap usage and OS-level memory consumption.

In this lesson, you will learn how to distinguish between the memory managed by the V8 engine and the total memory overhead reported by the operating system.

When you monitor your Node.js application, you will often encounter two primary metrics: v8.getHeapStatistics().used_heap_size and process.memoryUsage().rss. While both relate to memory, they represent fundamentally different layers of your application.

Understanding the V8 Heap

The used_heap_size reflects the memory currently occupied by JavaScript objects—the data structures, closures, and class instances managed by the V8 Garbage Collector (GC). If your application is leaking memory via retained object graphs, this is the metric that will grow over time. It represents the "language-level" footprint of your code.

Understanding Resident Set Size (RSS)

The rss (Resident Set Size) is a broader metric reported by the OS. It includes the entire segment of RAM occupied by your process, which consists of:

  • V8 Heap: The objects and code managed by the engine.
  • V8 Code Space: Compiled machine code and internal metadata.
  • External Memory: ArrayBuffer contents (like Buffer or Uint8Array) that reside outside the V8 heap.
  • Library Overhead: Loaded C++ modules, native dependencies, and the Node.js runtime itself.

Why the Gap Exists

A common point of confusion is seeing rss significantly higher than your heap usage. This is perfectly normal. If you allocate a 100MB Buffer using Buffer.alloc(100 * 1024 * 1024), the V8 heap may only increase by a few kilobytes (the reference to the buffer), but the rss will immediately jump by 100MB because the OS has allocated that raw memory block to your process.

const initial = process.memoryUsage();
const buffer = Buffer.alloc(50 * 1024 * 1024); // 50MB
const current = process.memoryUsage();

console.log(`RSS Growth: ${(current.rss - initial.rss) / 1024 / 1024} MB`);
console.log(`Heap Growth: ${(current.heapUsed - initial.heapUsed) / 1024 / 1024} MB`);

If you observe rss growing continuously while used_heap_size remains flat, you are likely dealing with external memory leaks or inefficient native module allocations rather than standard object retention.

Key Takeaway: The V8 heap tracks managed JavaScript object memory, whereas RSS represents the total physical RAM your entire process claims from the operating system, including buffers and runtime overhead.