When APM graphs show sawtooth heaps and GC pauses, guessing at flags doesn't help until you know what actually owns memory. In both Chrome and Node.js, V8 (not Node itself) compiles JavaScript, allocates objects, and runs garbage collection; Node provides runtime bindings, the I/O event loop, and default heap limits.
V8 memory spaces and the generational hypothesis
V8's collector is built around the generational hypothesis: most objects die young. Memory is split into:
- Stack & Code space: Primitives, pointers, and compiled JIT machine code.
- Young generation (~16 MB default): Small, fast space split into two semi-spaces (
From-spaceandTo-space) where newly allocated objects live (nursery->intermediate). - Old generation (~1.4 GB default max on 64-bit): Objects that survive multiple minor collections get promoted here.
- Large-object & metadata spaces: Oversized allocations that V8 avoids copying during collection.
Minor GC vs. Major GC
- Minor GC (Scavenge): Fast, frequent, parallel sweeps of the young generation. Live objects copy between semi-spaces and dead ones vanish.
- Major GC (Mark-Sweep / Mark-Compact): Less frequent, collects both young and old generations. Marking and sweeping run mostly concurrently/incrementally, while compaction rearranges fragmented memory and can cause noticeable stop-the-world pauses if the old generation grows huge.
Built-in Node.js inspection tools
v8module:v8.getHeapStatistics(),v8.getHeapSpaceStatistics(), andv8.writeHeapSnapshot()capture detailed heap breakdowns directly from code or an inspector REPL.process.memoryUsage(): Reportsrss(total OS resident memory),heapTotal, andheapUsed.- CLI flags:
--trace-gclogs every Scavenge and Mark-Sweep cycle with before/after sizes and pause durations;--max-old-space-size=<MB>and--max-semi-space-size=<MB>tune limits;--inspectexposes Chrome DevTools Memory profiling.
What this means for Next.js SSR
Server-side rendering allocates short-lived props, serialized payloads, and closures on every request:
- Keep shared cross-request caches out-of-process (or tightly bounded) so old-generation memory doesn't bloat and trigger long major GC pauses.
- Clear timers, listeners, and detached references; watch closures and
useEffectdependency arrays that silently pin large objects. - Avoid allocating huge transient strings (like unused raw response bodies) inside per-request fetch helpers.
- Trim unused dependencies, since fewer loaded server modules mean less retained state in memory.
References
- V8 documentation: memory leaks, tracing, and profiling.
- Node.js CLI & process docs