The garbage collection

The principle of

  • The execution environment will find thoseNo longerContinue using the variable thenThe release ofThe memory it occupies.

strategy

Reference counting garbage collection

The principle of

  • Keep track of each valuereferencedThe number of times

This algorithm simplifies the definition of “whether an object is no longer needed” to “whether the object has other objects referring to it”. If there are no references to the object (zero references), the object will be collected by the garbage collection mechanism.

limitations

  • A circular reference

Unable to handle circular reference cases. Two objects are created and referenced to each other, creating a loop. They leave function scope when called, so they are no longer useful and can be recycled. However, the reference-counting algorithm takes into account that they all have at least one reference to each other, so they are not recycled.

Mark clear

The principle of

Suppose you set an object called root (in Javascript, root is the global object). The garbage collector will periodically start at the root, looking for all objects referenced from the root, and then looking for objects referenced from the root. The garbage collector will find all available objects and collect all unreachable objects.

limitations

Objects that cannot be queried from the root object are cleared.

V8 garbage collection mechanism

Stack memory collection

When the stack context is switched, the space at the top of the stack is automatically reclaimed. The operating system automatically allocates and releases memory.

Collection of heap memory

New generation memory reclamation

  • Secondary garbage collector –Scavengealgorithm
  1. Marks active and inactive objects
  2. Copy live objects from space to to space and sort them
  3. Frees memory for inactive objects in from space
  4. Swap the roles of from space and to space

Old generation memory reclamation

  • Main garbage collector –Mark-Sweep & Mark-Compact
  1. Marking stage: The old generation is scanned for the first time, markingActive objects.
  2. Cleaning stage: The old generation is scanned for the second time to clear the unmarked objects, that is, cleaningInactive object.
  3. Active object collation stage, all active objects to one end of the movement, after the completion of the movement, directly clean up the memory outside the boundary.

Optimize the Orinoco

1. Incremental mark

Incremental tags break up the original tag heap object into tasks that are interspersed between JavaScript application logic.

Lazy cleaning

A pause is divided into several steps, each small step allowing the operation logic to run for a while, and so on.

3. The concurrent

4. The parallel

Conditions under which new generation objects are promoted to old generation

  1. The first is to determine whether the object has already passed through onceScavengeRecycling. If so, copy the object From the From space to the old generation. If not, it is copied To the To space.
  2. The second is whether the memory usage of the To space exceeds the limit. When an object is copied From the From space To the To space, if the To space usage exceeds25%, the object is directly promoted to the old generation. The reason for setting 25% is that after the algorithm ends, the two Spaces will switch positions. If the memory of the To space is too small, the subsequent memory allocation will be affected.