V8 memory partitioning

  • V8 is broadly divided intoThe heapandThe stackGarbage collection takes place in the heap.
  • Heap memory is divided into multiple modules:
    • New space

      This is where most objects are initially allocated. This area is relatively small but garbage collection is particularly frequent. This area is split in half (Semi space From and Semi Space To).

    • Old space

      After surviving for a period of time, objects in the new generation will be transferred to the old generation memory area, which has a lower garbage collection frequency compared with the new generation memory area.

    • Large object space

      Each object has its own memory, and garbage collection does not move the large object area.

    • Code space

      The code object is going to be allocated here, the only area of memory that has execution permission.

    • Cells pace

    • Property cell space

    • Map space

  • Garbage collection happens in the New space and the Old space.

Memory size of new generation and old generation

The value varies depending on the OPERATING system: 0.7 GB for 32-bit and 1.4 GB for 64-bit

The new generation The old generation
32 bit system 34M 700M
A 64 – bit system 64M 1400M

The latest version, V14, has 2 gigabytes of memory

Garbage collection algorithm

The Censors use the Scavenge.

The old generation now uses Mark-Compact, the old generation used mark-sweep, and the oldest is reference counting.

Scavenge

Process:

Each time a new variable is added, the garbage collection will start when the memory in the FROM is full: When the garbage collection is complete, the memory that still has references will be copied to the to. At the end of the garbage collection, the memory that still has references will be swapped between the FROM and to. Finally, the memory that has references will be swapped between the FROM and to.

Because “From” and “To” are half and half, and “To” is never stored, half of the space is wasted. This algorithm uses space to exchange time, and the old generation is much larger than the new generation, so it is not reasonable to use this algorithm.


Reference counting method

Process:

Check to see if the object is referenced by another object, and if not, clear it.

This method cannot handle circular references between objects and is prone to memory leaks.

It was abandoned in the IE era.


Mark-Sweep

This algorithm is an earlier one that is no longer in use

Process:

Garbage collection builds in a root node (think of it as window in the browser, gelobal in Node). First, all objects in the old generation are scanned in breadth, and the objects that have references to the root node are marked. Finally, unmarked objects are garbage collected.

This algorithm does not defragment memory, so garbage collection is triggered early when a large object is needed later. This problem was solved by the mark-Compact algorithm.


Mark-Compact

Optimized on the basis of the mark elimination method:

  • It solves the problem of running out of contiguity when large objects are stored by moving all marked memory together at the beginning of garbage collection and then cleaning up the unmarked memory.

  • The original Mark-sweep algorithm would carry out a Sweep in the garbage collection process and Mark all reference nodes, which is called full stop marking. Because JS is single-threaded, all marking within a garbage collection time would cost a lot of time. Now, incremental and tricolor notations are used.

Incremental notation and tricolor notation

Make switching between code execution and garbage collection more frequent, and only look down one level at a time for garbage collection.

The algorithm divides each node into three states:

  • White: not searched
  • Gray: The next time the search starts from here, the object referenced to this object is not found
  • Black: marked, the object referring to the object has already been looked up

A lookup is done at each garbage collection until the reference tree is all black before sorting – collection.

This pattern inserts each garbage collection tag separately into the code run, which is a better experience than the original full stop tag. (Incremental cleanup and deferred cleanup were also introduced later to make pauses shorter.)


The new generation rose to the old generation

The judgment is simple:

  • First determine if the memory has undergone a garbage collection, and if it has not, it will be placed in to.
  • Then determine if 25% (8M) of the TO has been used at this point, and if not, it will be put into the TO.
  • If both conditions are true, they will be promoted to the old generation.