Lesson 4 of 9 · 25m
Buffers, External Memory, and Who Owns the Bytes
Understand Buffer allocation, external memory accounting, string conversion costs, and the ownership mistakes that distort RSS and leak data.
The first time you graph Node memory honestly, it looks like the runtime is lying. heapUsed is calm while RSS climbs past the container limit. Often nobody leaked JavaScript objects; the bytes live outside V8's managed heap in buffers, native allocations, compression state, or addons. Production memory work starts with ownership: which allocator owns these bytes, and when are they released?
Buffer memory is not ordinary heap memory
Buffer objects represent binary memory outside the normal V8 object heap. V8 tracks enough external memory to influence garbage collection, but the bulk allocation is native:
const a = Buffer.alloc(1024 * 1024); // zero-filled
const b = Buffer.allocUnsafe(1024 * 1024); // may contain old memory
const c = Buffer.from('hello', 'utf8'); // copy from string bytesallocUnsafe is faster because it does not zero memory. It can expose stale bytes if you write it into a response before filling every byte. Use it only when you control initialization completely.
Strings and buffers are different formats
JavaScript strings are immutable text sequences. Buffers are bytes. Every conversion is a copy and may be expensive:
const body = Buffer.concat(chunks).toString('utf8');That pattern is correct for small bounded payloads and dangerous for unbounded ones. It stores every chunk, concatenates into another large allocation, then converts to a string. A moderately large JSON upload can multiply memory several times before your parser even runs.
Slices share memory
A slice is a view, not a copy:
const packet = Buffer.alloc(64 * 1024);
const header = packet.subarray(0, 64);Keeping header alive can keep the original 64 KB allocation alive. The same class of surprise appears in binary protocols, file parsers, and message queues. Copy when the retained portion is small and the backing allocation is large:
const retained = Buffer.from(packet.subarray(0, 64));Read the memory numbers correctly
const m = process.memoryUsage();
console.log(Object.fromEntries(Object.entries(m).map(([key, value]) =>
[key, Math.round(value / 1048576)]
)));RSS is what the operating system charges you. heapUsed is live JavaScript heap. external tracks memory associated with certain external objects. None of these alone tells the full story, especially once native libraries or worker threads enter the picture.
Garbage collection is not a memory allocator undo button
V8 can free objects only when they are unreachable. Caches, subscribers, global registries, closures, and in-flight requests are common retention roots. Also remember that freed native memory may remain in allocator arenas, so RSS can stay high even after pressure falls. The container memory limit does not care whether the allocator is merely being conservative.
Field notes
- Prefer explicit byte limits at trust boundaries before parsing JSON, decompressing archives, or concatenating chunks.
Buffer.concat(chunks)with no total length walks data twice and can allocate very large contiguous memory.- Treat memory growth after load stops differently from growth during load; steady-state growth is what usually indicates leaks.
- Native addons may allocate through
mallocand never appear cleanly in V8 heap snapshots.
Was this lesson helpful?