Application programs need to occupy the storage space of the system to load codes and store runtime data. Understanding the memory space management strategy gives us a better understanding of how code works on the system. This article step-by-step introduces V8’s garbage collection mechanism, starting with code runtime storage.

Runtime storage

Heap storage

  • Main store global variables, reference types
  • Dynamic allocation, allocatable dynamic space, garbage collection mechanism to participate in space management
  • The total storage space (usually limited space allocated to applications) is large, resulting in low search efficiency
  • Heap space is shared between applied threads

The stack is stored

  • Store local variables (basic data types), Pointers, function frames
  • Allocated by the system, usually to store data of a limited size, and freed when a stack fragment pops up
  • Stack structure last in first out (LIFO), high access efficiency
  • Multithreaded applications have one stack storage space per thread

Memory management

Memory management usually refers to the management of heap memory space, and here’s how it works in V8.

Programs running in V8 are Resident Set with the following memory space

V8 heap memory structure

  • Newly allocated objects or objects of short duration are stored in the New Space
  • (Old Space) New Space can be promoted to Old Space after garbage collection, divided into Old Pointer Space (save objects pointing to other objects) and Old Data Space (save other objects promoted from Old Space).
  • Large object space stores objects larger than other areas
  • (code-space) Code area stores Code, the only storage space that has permission to run
  • Cell space, property Cell space, and Map space these Spaces hold objects of the same size

V8 Garbage collection

V8’s garbage collection mechanism only applies to the new and old areas of memory space. Due to the different types of data stored in the new and old areas (size, lifetime), and so on, garbage collection is implemented in the new and old areas using different strategies.

New area garbage collection

Freshmen use the Scavenger algorithm

  • There is only one semispace in use at runtime. The semispace in use is called From space, and the semispace in idle state is called To space
  • Objects are allocated From the From space first, and garbage collection is triggered when the From space cannot store enough new objects
  • During garbage collection, the surviving objects in the From space are checked and copied To the To space. After the replication, the roles of the From space and To space are switched

The freshman section was promoted to the old section

In the new area garbage collection meets the following two conditions, can be moved to the old area storage.

  • When an object is copied From the From space To the To space, the object is copied From the From space To the old block if it has been Scavenge
  • When an object is copied From the From space To the To space, if more than 25% of the To space is used, the object is copied directly To the old space

Old area garbage recycling

The Scavenge algorithm is not scientific because of the large number of existing exploiture in old age areas. Mark-sweep-compact is used for garbage collection in old-growth areas.

  • Mark only marks objects that are alive and will be cleared if they are referenced in a loop but cannot be marked.

    • The garbage collector internally creates a root list (global objects, local variables and arguments of local functions, and variables and arguments of other functions currently nested in the call chain) that can be used from the root node to find variables that can be accessed
    • The garbage collector starts at all the root nodes and walks through accessible child nodes marked as active and unreachable nodes as inactive
  • Sweep frees space for inactive nodes

  • Since garbage collection suspends the execution of applications, V8’s garbage collection mechanism includes incremental GC, Concurrent marking, Concurrent cleaning, and Lazy Jie and other means are combined to optimize recovery efficiency.

    reference

    Demystifying memory management in modern programming languages Visualizing memory management in V8 Engine (JavaScript, NodeJS, Deno, WebAssembly) find out about garbage collection for V8 engines