NodeJs Internals: V8 & Garbage Collector
- Application heap out of memory
- Use — trace_GC to track garbage collection
- Use process.memoryUsage to view heapUsed, heapTotal
- v8-profiler
- node-heapdump
- node-memwatch
- Node.js (V8) GC mechanism
- Heap memory is divided into [[new-space][old-space]]
- Generation memory space: Save objects that have a short lifetime
- Old-generation memory space: Holds objects that live for a long time or reside in memory
- Scavenge algorithm: an algorithm for recycling garbage from new generation objects
- Adopt the way of copy.
- The heap memory is split in two and the two parts become semisapce
- From Space: Semispace in use
- To Space: Idle Semispace
- When allocating memory, allocate it From the From space first.
- When garbage collection is performed, live objects in the From space are checked, and these live objects are copied To the To space, and non-live objects are freed.
- After the replication is complete, the roles of the From space and To space are reversed.
- Objects surviving in the new generation will be transferred to the old generation, known as promotion, if:
- The subject has undergone a Scavenge
- To space usage exceeds 25%
- Mark-sweep: Garbage collection algorithm for old generation objects
- Mark clearance, divided into two phases
- Marking phase: Traversing all objects in the heap, marking living objects
- Clear phase: Clears unmarked objects
- After each clear mark, memory space is discontinuous, memory fragmentation.
- If memory needs to be allocated to a large object, it is possible that all of the fragmentation space will not be allocated and garbage collection will be triggered.
- Mark clearance, divided into two phases
- Mark-compact: Optimized memory fragmentation based on Mark-sweep
- When the marking phase is over, move all living objects to one end
- After the move is complete, it clears all memory on the other side
- Stop-the-world: GC needs to pause your JavaScript execution code to complete garbage collection, V8’s solution:
- Incremental-marking: Incremental Marking to divide a long Marking task for GC into multiple iterations
- Lazy-sweeping: Lazy Sweeping, as long as the current free memory is large enough to run the current code
- Concurrent: V8 makes use of multiple threads (workers) for simultaneous token clearing
- Parallel: When JavaScript code is executed, the GC is marked Parallel to it to avoid stop-the-world
- Heap memory is divided into [[new-space][old-space]]