In this lesson, you will learn how to distinguish between the New, Old, and Large Object spaces to accurately diagnose memory pressure in your Node.js applications.
V8 divides the heap into distinct regions to optimize garbage collection (GC) cycles. Understanding which region is "stressed" allows you to determine if your application is suffering from short-lived object churn or a long-term memory leak.
1. New Space (Young Generation)
This is where almost all objects are initially allocated. It is small, typically ranging from 1MB to 8MB. Because it is optimized for fast allocation and quick "Minor GC" cycles, it uses a semi-space design. If your memory profile shows high frequency, short-lived spikes here, your application is likely creating many temporary objects (e.g., inside hot loops).
2. Old Space (Old Generation)
Objects that survive multiple GC cycles in the New Space are "promoted" to the Old Space. This region is significantly larger and is managed by "Major GC" (Mark-Sweep-Compact). If you see the Old Space growing steadily over time, you are likely dealing with a memory leak—references are being held, preventing objects from being collected.
3. Large Object Space
Objects exceeding a specific size limit (typically ~1MB) bypass the New Space and are allocated directly here. These objects are never moved by the GC because the cost of copying them would be prohibitive.
Visualizing Memory Pressure
When analyzing a heap snapshot, you can look for the source of pressure:
// A pattern that fills New Space rapidly, potentially triggering frequent Minor GC
for (let i = 0; i < 1e6; i++) {
const temp = { id: i, data: new Array(100) };
}
// A pattern that fills Old Space, leading to Major GC and potential pauses
const cache = [];
function addToCache(obj) {
cache.push(obj); // Persistent reference causes growth in Old Space
}
If your process.memoryUsage().heapUsed grows rapidly, check if your objects are "living" long enough to be promoted. High frequency of Major GC indicates that the Old Space is reaching its limit, which often leads to the latency spikes we discussed in earlier performance modules.
Key Takeaway: Monitor New Space for churn caused by short-lived objects and Old Space for sustained growth indicating potential memory leaks or inefficient object retention.