V8 garbage collection simple record

Garbage collection mainly involves performing garbage collection in stacks and heaps,

In stack cases, ESP (the pointer to the current stack) moves to the next execution context, which overwrites the previous execution context.

The heap is treated with a generational garbage collection mechanism:

The heap is divided into two parts, the new and the old. The new area has much less memory than the old area because the new area does a lot of garbage collection.

New area:

The Reclamation uses the Scavenge method

We will be the new area mainly is divided into two parts, one is called variable area, what is referred to as a free area, we first put the object variable area, such as variable area at the end of full garbage collection mechanism, variable variable area to copy to the free zone, the variable area to clean, variable area becomes free area at this moment, the original free area becomes a variable area.

There is also a variable promotion mechanism in the new area, when the variable is still in the new area after several baptisms, it can be put into the old area.

Old living area:

Old living area basically is deposit variable big, perhaps live long variable.

The Scavenge avenge method is not appropriate because of the relatively large size of the variables. Therefore, in the old area, we mainly use mark clearing and mark sorting algorithm.

First, mark the variables that can be reached in the old area, then collate the variables and put them in, and finally clear the object space outside the collate. Because using only the token-clearing algorithm can cause a lot of memory fragmentation, storing large objects later can be troublesome.

JS, of course, because of the single thread, if we put the clear marking to sort out the things to go to processing, obviously hinders other JS code to run, so that will cause the user feel obviously card phenomenon, let the mark phase alternating with JS, so that it can be a very good feeling to reduce garbage collection. Improve the experience.