preface

Every language has its own garbage collection mechanism, some need to manually clean up some automatic cleaning, and JS garbage collection is automatic, but how does it work? Let’s break it down step by step

How does JS generate garbage data

We know that memory in JS is made up of stacks, for example IF I define an A object on a window

window.a = { name: 'hello' };
Copy the code

In memory it looks like thisThe stack is usually the reference address of the stored object, and the heap is the data of the object, if we change the code to this

window.a = new Date();
Copy the code

In this case, a’s reference address is the address of the new Date() data

In this case, the original Object has become garbage data. How does GC clean up garbage data

Data cleaning and recycling

First we introduce two concepts: live objects: GC traverses objects from the Root, and GC marks the objects it accesses as live objects. 2 Inactive objects: if the bug GC Root doesn’t traverse, it marks them as inactive objects

GC Root in the browser window object, such as storage on the stack variables such as these, in order to improve the efficiency of garbage collection in the heap space actually will heap memory is divided into two interval is a new area a is an old area, new district is the difference between them is mainly responsible for some survival time shorter data objects such as function of internal variables, When a function is destroyed, object data is also destroyed, usually taking up 1 to 8 meters less space, while long-lived and large data objects are placed in the old area

The garbage collector is also different for different regions

The freshman area mainly hasSecondary garbage collectorBe in charge of old living area as long as beMain garbage collectorAnd their respective recycling algorithms and mechanisms are different. Let’s take a look at the recycling methods of garbage collectors in the two regions

New area

The reclamation algorithm is the Scavenge algorithm, which splits a reclamation area into two areas, the object area and the free area. New objects are placed on the object area, and when objects in the object area are filled up, a GC is performed to mark the active objects in the object area. The subgarbage collector then copies the live objects to the free area in a certain order, and then reverses the two areas when the copy is complete. The original object area becomes the free area, and the original free area becomes the object area. The secondary garbage collector does this repeatedly, with an object promotion strategy that copies objects that have been placed in the object area multiple times into the old area, and that’s all the secondary garbage collector does

Old living area

The old area uses mark-sweep algorithm to directly Sweep the marked inactive objects in the old area. However, after many times of cleaning, the space in memory will be discontinuous, and this will be a problem when a piece of continuous storage space is needed. So the main garbage collector introduces a mark-compact algorithm to move live objects to one end and then clean up the data on the other end, freeing up memory as shown below