AI Briefing
KO

Garbage Collection Principles in the V8 Engine: How Minor GC and Major GC Work

·2022.05.19 09:00

Key point

The V8 engine divides the heap into New and Old spaces, managing them with the Scavenger and Mark-Sweep-Compact algorithms, respectively.

1 / 13

Details

The JavaScript V8 engine performs Garbage Collection (GC) to automatically manage memory without requiring developers to manually deallocate it. This process is based on the Generational Hypothesis, which separates heap memory into New space and Old space, applying GC methods optimized for the characteristics of each region.

Minor GC and the Scavenger Algorithm

New space is the region for storing newly created objects, where most objects have short lifecycles. Minor GC uses the Scavenger algorithm to alternate between two Semi spaces (From space and To space) within New space, evacuating objects. Surviving objects are moved to contiguous memory to prevent memory fragmentation, and objects that survive two Minor GC cycles are promoted to Old space.

Major GC and Tri-color Marking

Old space is the region for storing long-lived objects, managed by Major GC through the Mark-Sweep-Compact algorithm. During the marking phase, the Tri-color algorithm (White, Gray, Black) is used to track reference relationships. In the Sweeping phase, unreferenced white objects are registered in the Free-list to reclaim memory. In the final Compact phase, pages with severe fragmentation are rearranged to improve memory efficiency.

The Orinoco Project and Performance Optimization

Traditional GC causes Stop-the-world pauses that halt program execution, potentially degrading user experience (UX). To address this, V8 evolved GC through the Orinoco project. Key optimization techniques include Parallel, which distributes work using helper threads; Incremental, where the main thread processes work intermittently; and Concurrent, where helper threads perform GC to eliminate main thread pauses. Additionally, embedders like Chrome provide an Idle-time GC mechanism that utilizes idle time to trigger GC.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.