directory

  • Know the V8
  • V8 Garbage Collection Policy
  • V8 commonly used GC algorithm
  • Generation recovery of V8

    • V8 memory allocation
    • Cenozoic object collection

      • Main algorithms used
      • The recycling process
      • promotion
    • Recycle the old generation object

      • Main algorithms used
      • How do tag increments optimize garbage collection?
    • The new vs. the old

Know the V8

  • V8GooglePublish mainstream open sourceJavaScriptEngine, usingC++To write. Adopt just-in-time compilation, direct translation into machine language, and use inline caching (inline caching) to improve performance. With these features,JavaScriptThe programV8The engine runs at a speed comparable to binary programs.
  • V8 memory limit:

    • 64bitNo more than on the operating system1.5 G
    • 32bitNo more than on the operating system800M

    The limits are set so that the browser can use enough memory, there is a spamming mechanism inside, and the time is within a reasonable range of user perception

  • At presentV8Garbage collection is required by an incremental marking algorithm50ms, the non-incremental marking algorithm is required1s

V8 Garbage Collection Policy

In the process of program use, can be divided into primitive type data and object type data.

The original data is controlled by the program language itself, and the collection here refers to the object data that mainly lives in the heap area. This process is inseparable from the memory operation, and V8 also sets a limit on the memory. How to collect garbage in this case?

  • The idea of generation recovery is adopted
  • Memory is divided into new generation storage area, old generation storage area
  • Different algorithms are used for different generations

Therefore, V8 will use more GC algorithm, here do not understand the GC algorithm and this article mentioned in the tag cleaning, sorting and other algorithms are introduced in 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)
  • Space to copy
  • Mark clear
  • Tag to sort out
  • Mark increments (for efficiency)

Generation recovery of V8

  • New generation — objects that have a short life span, such as a local scope in which variables are collected as soon as the function completes.
  • Old generation – refers to objects that have long lived, such as global objects, closed variable data.

V8 memory allocation

The memory space of V8 is divided into two parts, the new generation storage area and the old generation storage area, as shown in the figure below:

  • The small space on the left is used to store the new generation of objects

    • 64bitNo more than on the operating system32M
    • 32bitNo more than on the operating system16M
  • The larger space on the right is used for storing old generation objects

    • 64bitNo more than on the operating system1.6 G
    • 32bitNo more than on the operating system700M

Cenozoic object collection

Main algorithms used

Adopting the assignment algorithm + the tag sorting algorithm

The recycling process

The new generation memory is divided into two equally sized Spaces, using space for From and free space for To.

If you need to apply for space, the recycling steps are as follows:

  1. First, all live objects are stored inFromSpace, in the processToIs idle state.
  2. whenFromIt’s triggered after a certain amount of space is usedGCAt this time, the active object will be marked and moved to make the use space become continuous, so that the subsequent fragmentation space will not be generated.
  3. Copy the live object toToSpace. After the copy is completed, there is a backup of the active space. This is the time to consider recycling.
  4. theFromThe space is released and the collection is complete
  5. rightFromandToSwap the names and continue with the previous operation.

To sum it up:


Using the From ->
Trigger GC tag collation ->
Copy To the To ->
Recovery From ->
Name interchange before repeating

promotion

The reference of an object in the process of copying may be promoted in the proterozoic space. Promotion is about moving the new generation into the old.

When is the promotion action triggered?

  1. roundGCThe next generation of survivors need to be promoted
  2. In the copying process,ToSpace usage exceeds25%Move all the active objects to the old generation space

Why limit the use of To?

The future collection operation is going to take
FromCopy the contents of the space to
ToI’m going to swap in space, if
ToThe utilization rate is too high to become
FromAfter that, new objects can’t be saved.

Recycle the old generation object

Main algorithms used

The main methods are mark clearance (primary), mark collation and incremental mark algorithm

  • Mark clear: Although there are space fragmentation issues with tag cleanup, tag cleanup can improve very quickly.
  • Tag to sort out: When promotion occurs and there is not enough space in the Old Age area, it will be carried out by markingSpace optimization.
  • Incremental tag: The garbage collection operation mark of a whole section is split into several small sections to complete the collection. The main purpose is to realize the alternating completion of the program and garbage collectionThe efficiency of optimizationThe time consumption is more reasonable.

The principle of tag cleanup and collation was explained in the GC article. Here’s how incremental markup works in more detail.

How do tag increments optimize garbage collection?

Garbage collection can be divided into two parts, one is the execution of the program, the other is garbage collection. When garbage is collected, it actually blocks the execution of the program, so there are empty slots in between.

The new vs. the old

  • The new generation of regional garbage collection uses space for time

    • Replication algorithm is mainly adopted, and there should be free space. Of course, the new generation itself has a small space, so the space allocated for replication is even smaller, so the efficiency of wasting this space in exchange for time is negligible
  • The old generation area garbage collection is not suitable for replication algorithm, because the old generation space is divided into two parts, which will cause half of the space waste, and the stored data will take a long time to copy.