preface

In my spare time over the weekend, I briefly read node.js in Its simplest form. I feel that this book is very deep, but I don’t know much about Node. I still want to know about the garbage collection mechanism useful for interviews.

V8’s garbage collection mechanism

Why is there a garbage collection mechanism, memory limits. With default Settings, if memory is always allocated, only about 1.4GB and 0.7GB can be used on 64-bit and 32-bit systems, respectively.

Garbage collection includes data collection for the call stack and data collection for the heap. Garbage collection of the call stack is typically done through a pointer to the current execution state (ESP). Moving the pointer down destroys the execution context of the function. So, let’s take a closer look at garbage collection in the heap, what we call V8’s garbage collection mechanism.

V8’s garbage collection strategy is based on generational garbage collection (the intergenerational hypothesis).

  • Most objects live in memory for a short time
  • Undead objects live longer

The main memory is divided into the new generation and old generation of two generations.

The new generation

New generation: Scavenger algorithm is used for garbage collection of objects with short storage and survival time. It is a garbage collection algorithm implemented by replication.

  • It splits heap memory into two parts, with the used space called From space and the idle space called To space.
  • The newly created objects are first allocated To the From space. During garbage collection, the living objects in the From space are copied To the To space, and the space occupied by the non-living objects is released
  • After the replication is complete, the roles of the From space and To space are reversed

Since the algorithm only copies live objects, it has high garbage collection efficiency in the frequent creation of temporary objects.

The old generation

Objects that live long or are resident in memory. When an object survives multiple copies, it is moved to the old generation and a new algorithm is used for garbage collection. The process of moving objects from the new generation to the old generation is called promotion.

The object can be promoted if one of the following conditions is met:

  • 1. Whether the object has experienced Cenozoic replication
  • 2. When an object is copied From the From space To the To space, the To space has been used more than 25%.

Mark-and-sweep

The objects in the old generation are no longer suitable for Scavenger algorithm. First, there are many living objects and the replication efficiency is low. Second, the space is divided into two, wasting half of the space.

Mark clearIn the marking phase, the garbage collector recursively marks every accessible object at runtime. In the sweep phase, only unmarked objects are cleared. However, a problem caused by mark clearing is that after a mark clearing, the memory space will appear discontinuous state, resulting in the next allocation of large memory objects and not fit, so the introduction of mark clearing.

Tag to sort out

To move a living object to one end of a detangling process, and when the move is complete, clean up memory directly beyond the boundary.

Incremental tag

In order to avoid the inconsistency between the JS application logic and the garbage collector, the three garbage collection algorithms need to suspend the application logic and resume the application logic after the garbage collection. This behavior is called “total pause”. Old generation space allocation is very large, a recycling time will be very long, which will lead to page lag. Of course, this behavior was a long time ago. V8 introduces incremental markup, where JS application logic is executed alternately with garbage collection.

Garbage collection algorithm comparison

In V8’s recycle strategy, both are used together. Here is a comparison of the three algorithms

conclusion

Interviewer: Can you talk about V8’s garbage collection mechanism?

V8’s garbage collection strategy is 1) most objects live very short and 2) undead objects live more

Heap memory is divided into new generation and old generation space

The Scavenge algorithm, used by the New generation, is small and has a split memory space; In garbage collection, live objects are copied to the free side, clearing up the original space.

In the old generation, the algorithm of tag clearing and tag sorting was adopted. Mark clearing marks all living objects and clears unmarked objects. Memory fragmentation is then left behind, and mark-up is then used to move surviving objects aside and clean up memory beyond the boundary.

The last

Personally feel that such pure theoretical article, are the same, complete reference to “simple Node.js”, ha ha, but after writing a article, I think the interview was asked this piece, high and low can be the whole sentence. So, we should send more articles, (, ´ ω•) blue “(´っω• ‘).