directory

  • Know the V8
  • V8 garbage collection policy
  • V8 commonly used GC algorithm
  • V8 generational recycling
    • V8 memory allocation
    • New Generation Object Reclamation
      • Mainly using algorithms
      • The recycling process
      • promotion
    • Recycle old generation objects
      • Mainly using algorithms
      • How do markup increments optimize garbage collection?
    • New generation VS old generation

Know the V8

  • V8 – is the mainstream open source JavaScript engine released by Google, written in C++. It uses just-in-time compilation, translates directly into machine language, and uses methods such as inline caching to improve performance. With these features, JavaScript programs can run as fast as binary programs under the V8 engine.

  • V8 Memory limits:

    • 64bitThe operating system does not exceed1.5 G
    • 32bitThe operating system does not exceed800M

    These limits are set so that the browser uses enough memory, has an internal garbage mechanism, and the time is within a reasonable range of user perception

  • At present, it takes 50ms for V8 garbage collection to adopt incremental labeling algorithm and 1s for non-incremental labeling algorithm

V8 garbage collection policy

The process of using the program can be divided into primitive type data and object type data.

The original data is controlled by the programming language itself. In this case, recycling refers to the object data that mainly lives in the heap. This process is inseparable from memory operations.

  • Adopt the idea of generational recycling
  • The memory is divided into new generation storage area and old generation storage area
  • Different algorithms are used for different generations

So V8 will use more GC algorithm, here is not understand the GC algorithm and this article mentioned in this article, such as mark clearing, sorting algorithm detail in this article, this article will not be repeated

GC — Garbage collection mechanism understanding and algorithm detailed explanation

V8 commonly used GC algorithm

  • Generational recycling (definitely used)
  • Space to copy
  • Mark clear
  • Tag to sort out
  • Mark increments (for efficiency)

V8 generational recycling

  • Generation – objects that have a short lifetime, such as a local scope where variables are reclaimed as soon as the function finishes executing.
  • Old generation – objects that are long lived, such as global objects, closure variable data.

V8 memory allocation

The MEMORY space of V8 is divided into the new generation and the old generation, as shown in the figure:

  • The small space on the left is used to store new-generation objects
    • 64bitThe operating system does not exceed32M
    • 32bitThe operating system does not exceed16M
  • The larger space on the right is used to store old generation objects
    • 64bitThe operating system does not exceed1.6 G
    • 32bitThe operating system does not exceed700M

New Generation Object Reclamation

Mainly using algorithms

The assignment algorithm + tag sorting algorithm is adopted

The recycling process

The new generation memory is divided into two equal size Spaces: the used space is From, and the free space is To.

To reclaim space, perform the following steps:

  1. All active objects are first stored inFromSpace, this processToIs idle.
  2. whenFromIt’s triggered after a certain amount of space is usedGCOperation, this is when you tag the live object and move it around to make the use space continuous so that it doesn’t fragment the space later.
  3. Copy the live object toToAfter the copy is complete, the active space will have a backup. At this time, you can consider recycling operations.
  4. theFromThe space is released and the recovery is complete
  5. rightFromandToChange the name and continue the operation.

To summarize: use From -> trigger GC tag collation -> copy before To -> recycle From -> name interchange repeats

promotion

In the process of copying a reference to an object in the old generation space, a promotion may occur. Promotion is the movement of the new generation to the old generation.

When is a promotion triggered?

  1. roundGCThe next generation that survives will need to be promoted
  2. During the copying process,ToThe space usage exceeds the threshold25%, move the active objects to the old generation space

Why limit the use of To?

Future reclamation is To copy the contents of the From space into the To space for exchange. If the To usage is too high, the new objects will not be stored after becoming From.

Recycle old generation objects

Mainly using algorithms

It mainly adopts the algorithm of mark clearing (first), mark sorting and incremental mark

  • Mark clearAlthough there is a problem with space fragmentation using tag cleanup, the rate of improvement of tag cleanup is very fast.
  • Tag to sort out: In the promotion time and the old generation area space is not enough to accommodate, will use the mark sortingSpace optimization.
  • Incremental tag: The whole section of garbage collection operation mark is divided into several small sections to complete the collection, mainly in order to achieve the program and garbage collection alternately complete, soThe efficiency of optimizationThe time consumption is more reasonable.

The principles of tag clearing and marshaling were explained earlier in the GC article. Here’s a more detailed look at how incremental markup works.

How do markup increments optimize garbage collection?

The graph can be divided into two parts of garbage collection, one is the execution of the program, one is garbage collection. Garbage collection actually blocks execution of the program, so there’s a window in between.

New generation VS old generation

  • The new generation of regional garbage recycling useSpace for time
    • Mainly using the replication algorithm, there should be free space, of course, the new generation itself is small space, the partition of the replication space is smaller, so waste this space in exchange for time efficiency is negligible
  • Old generation garbage collection is not suitable for the replication algorithm. The old generation space is large and divided into two parts. Half of the space is wasted, and the storage data is copied for a long time.