How V8’s Scavenger and Mark‑Sweep Algorithms Optimize JavaScript Memory Management
This article explains V8’s generational heap layout, the Scavenger minor‑GC algorithm with its marking, evacuation and pointer‑updating steps, the parallel Scavenger enhancements, the major mark‑and‑sweep collector with optional compaction, and the various triggers that decide when each garbage‑collection phase runs.
1. Generational Layout
The V8 heap is divided into a young generation (new space and intermediate space) and an old generation based on the generational hypothesis, which assumes most objects die quickly. New objects are allocated in the new space; surviving objects are promoted to the intermediate space and eventually to the old generation.
2. Scavenger Algorithm (Minor GC)
Minor garbage collection uses the Scavenger algorithm, which consists of three interleaved steps: marking, evacuation, and pointer updating.
2.1 Marking
The collector starts from GC roots, traverses the object graph, and marks all reachable objects in the young generation.
2.2 Evacuation
Marked objects are copied from the From‑Space to the To‑Space (or to the old generation if they have been promoted). V8 uses a semi‑space design where the young generation is split into two equal halves; after evacuation the roles of From‑Space and To‑Space are swapped.
2.3 Pointer Updating
All references to the moved objects are updated to point to their new locations. Write barriers record cross‑generation references to avoid scanning the entire old generation.
3. Parallel Scavenger
V8’s parallel Scavenger maintains a global work list. Multiple threads steal tasks from this list, allowing concurrent marking, evacuation, and pointer updating. A barrier mechanism prevents unsafe parallel work, and the approach reduces minor‑GC pause time by about 55%.
4. Mark‑Sweep Algorithm (Major GC)
The major collector uses the classic mark‑and‑sweep algorithm, optionally followed by compaction.
4.1 Marking Phase
V8 employs a three‑color marking scheme (white, gray, black) with a marking worklist. Write barriers keep the marking correct when the main thread mutates objects during concurrent marking.
4.2 Sweeping Phase
After marking, dead objects are reclaimed and their memory is added to size‑classified free lists for future allocations.
4.3 Compaction Phase (Optional)
If fragmentation exceeds a heuristic threshold, live objects are moved to contiguous memory pages, eliminating gaps. This phase runs in parallel but blocks the main thread to avoid data races.
5. Garbage‑Collection Triggers
Minor GC runs when the young generation fills up, when allocation fails, or during idle periods. Major GC is triggered when the old generation exceeds a dynamic memory limit, when overall heap usage passes a threshold, or when a page becomes highly fragmented. In low‑memory mode, thresholds are lower, causing more frequent collections and up to 50% reduction in heap size on devices with less than 512 MB RAM.
6. References
node/deps/v8/src/heap/marking.cc node/deps/v8/src/heap/sweeper.cc node/deps/v8/src/heap/mark-compact.cc node/deps/v8/src/heap/scavenger.ccV8 blog series: “Getting garbage collection for free”, “Jank Busters”, “Orinoco”, “Concurrent marking”, “Pointer Compression”, etc.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent Docs Tech Team
Based on years of technical expertise from the Tencent Docs team, Tencent Docs Tech shares the front‑store/back‑factory architecture model, the Kaicong atomic collaborative editing engine, large‑scale service practice insights, continuous infrastructure development, AI assistant innovation, and expertise in specialized format editing and massive social collaboration, driving a new revolution in the document space.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
