V8, Memory & Performance
V8 execution, garbage collection, memory leaks, optimization, profiling, and tracing.
Q65: How does V8 handle Garbage Collection in Node.js?
V8 uses a generational garbage collector dividing memory into Young Generation (Scavenge algorithms for short-lived objects) and Old Generation (Mark-Sweep-Compact for long-lived objects).
- Generational memory structure: Young and Old Generation.
- Uses Scavenger for quick short-lived sweeps and Mark-Sweep for long-lived objects.
Which garbage collection algorithm handles short-lived objects in V8's Young Generation?
Q66: How do you increase memory limits for a Node.js process?
Pass the --max-old-space-size=<size_in_mb> V8 flag during startup (e.g., node --max-old-space-size=4096 app.js) to expand the heap limit.
- Adjust V8 max heap boundaries via
--max-old-space-size. - Prevents Out-Of-Memory (OOM) crashes during large data processing.
Which CLI option sets maximum V8 Old Generation memory capacity to 4GB?
Q67: What causes memory leaks in Node.js applications?
Common causes include uncleaned global variables, active timer closures (setInterval), uncleared event listeners, and growing caching objects held in memory.
- Retained object references prevent V8 garbage collection.
- Common sources: globals, forgotten event listeners, timers.
Which tool can inspect memory heap snapshots of a Node.js process?
Q69: How does the V8 execution pipeline (Ignition & TurboFan) execute Node.js JavaScript?
V8 uses an interpreter named Ignition to parse JavaScript source code into bytecode for immediate execution. As functions run repeatedly, V8 collects profiling data (type feedback). If a function becomes 'hot', the TurboFan optimizing compiler converts the bytecode into highly optimized machine code. If runtime type assertions fail, V8 performs deoptimization and falls back to bytecode.
- Ignition compiles JavaScript source into bytecode for quick initial startup.
- TurboFan compiles hot functions into machine code, deoptimizing if assumptions change.
What is the role of V8 TurboFan in Node.js runtime execution?
Q70: What is V8 Code Caching and how does it speed up Node.js application cold starts?
When Node.js compiles JavaScript files into V8 bytecode, it can generate a binary cache of the compiled bytecode. On subsequent application cold starts, V8 loads the cached bytecode directly from disk via v8.compileFunction() or Module Code Caches, bypassing source code parsing and compilation phases entirely to accelerate cold start boot times.
- V8 Code Caching saves compiled bytecode to disk for reuse across application boots.
- Reduces startup latency by bypassing source parsing and bytecode generation phases.
Which compilation phase is bypassed when Node.js loads a file with V8 Code Cache available?
Q71: How do you profile CPU usage in a Node.js application?
Run the application with the --prof V8 flag, perform load operations, and analyze the resulting tick log file using node --prof-process isolate-*.log.
- Generate V8 profiling logs using
--profflag. - Process output tick logs to identify performance bottleneck functions.
Which command-line flag enables V8 built-in CPU profiling tick logs?
Q72: How does the --trace-events-enabled flag assist in deep performance profiling?
Launching Node.js with --trace-events-enabled records performance events across V8, Libuv, and Node.js core modules into log files. These trace log files can be imported directly into Chrome DevTools (chrome://tracing) or Speedscope to inspect detailed thread execution timelines, garbage collection pauses, and async task durations.
--trace-events-enabledoutputs low-level execution trace logs.- Trace files can be rendered in Chrome DevTools to visually inspect V8 and thread pool performance.
Which Chrome DevTools panel or URL is used to visually inspect trace event log files generated by Node.js?