A directory

What’s the difference between a free front end and a salted fish

directory
A directory
The preface
Triple stack garbage collection
Four piles of garbage to recycle
Cenozoic and old generation
 5.1 New Generation – Secondary garbage collector
 5.2 Old Generation – Main garbage collector
Six full stop
Vii References

The preface

Returns the directory

Raw data types are stored in stack space, and reference data is stored in heap space.

However, after some data is used, it may no longer be needed. We call this data junk data.

If this garbage data is kept in memory, memory will be used more and more.

So we need to recycle these garbage data to free up the limited memory space.

This is where garbage collection comes in.

Garbage collection strategies are divided into manual collection and automatic collection:

  • Manual recovery: C/C++ can passfree()This code determines when memory is allocated and when memory is destroyed.
  • Automatic collection: Garbage data generated by JavaScript, Python, Java, and so on is released by the garbage collector.

Triple stack garbage collection

Returns the directory

When the function completes execution, the JavaScript engine destroys the execution context (variable environment, lexical environment, this, outer) stored in the stack by moving the ESP pointer down (the pointer that registers the current execution state of the call stack).

If there is code:

function foo(){
  var a = 1;
  var b = { name: 'jsliang' };
  function showName(){
    var c = 2;
    var d = { name: 'zhazhaliang' };
  }
  showName();
}
foo();
Copy the code

In the code above, according to the browser policy:

  1. Start with the global execution context
  2. And then tofooFunction execution context
  3. The last toshowNameFunction execution context

According to the characteristics of the stack, the JavaScript situation should be:

The call stack
showNameFunction execution context
fooFunction execution context
Global execution context

Then the release process begins:

  1. Perform toshowNameFunction when createdshowNameFunction execution context, and willshowNameThe execution context of a function is pushed onto the stack.
  2. At the same time, we have a pointer to the current execution state (calledESP) points to the call stackshowNameThe execution context of a function, indicating that it is currently executingshowNameFunction.
  3. whenshowNameAfter the function is executed, the function execution process entersfooFunction, at which point ESP moves down tofooThe execution context of a function. The move down operation is destructionshowNameFunction executes the procedure of the context.

If the showName function is followed by another call, it overwrites the contents of the showName function and holds the execution context of the other function.

That is, when a function finishes executing, the JavaScript engine destroys the execution context in the save stack by moving ESP down.

Four piles of garbage to recycle

Returns the directory

Above, we collected garbage from the stack via ESP:

function foo(){
  var a = 1;
  var b = { name: 'jsliang' };
  function showName(){
    var c = 2;
    var d = { name: 'zhazhaliang' };
  }
  showName();
}
foo();
Copy the code

But the b and D inside are still in the heap.

To collect garbage data from objects, you need a garbage collector in JavaScript.

Cenozoic and old generation

Returns the directory

Characteristics of intergenerational hypothesis:

  1. Most objects are short-lived
  2. Objects that are not destroyed live longer

These two characteristics apply not only to JavaScript, but also to most dynamic languages, such as Java, Python, and so on.

In V8, the heap will be divided into two regions: new generation and old generation. The new generation stores objects with a short life time, and the old generation stores objects with a long life time.

The new area is not as large as the old area, so V8 uses two different garbage collectors to efficiently implement garbage collection:

  • Secondary garbage collector. Mainly responsible for the new generation of garbage recycling.
  • Main garbage collector. Mainly responsible for old generation garbage recycling.

5.1 New Generation – Secondary garbage collector

Returns the directory

Algorithm: Scavenge

Object area The free zone
. .

Principle:

  1. The Cenozoic space is divided into two regions, one is the object region and the other is the idle region.
  2. New objects are stored in the object area. When the object area is nearly full, a garbage cleanup operation is performed.
  3. The garbage in the object area is marked first, and when marked, the surviving objects are copied to the free area
  4. After the replication is complete, the roles of the object area and the free area are reversed, that is, the original object area becomes the free area, and the original free area becomes the object area.

Object promotion strategy: Objects that survive two garbage collections are moved to the old area.

5.2 Old Generation – Main garbage collector

Returns the directory

  • Algorithm: Mark-sweep algorithm
  • Principle:
  1. Tagging: The tagging phase is to start with a set of root elements and recursively traverse the set of root elements. In this traversal process, the elements that can be reached are called active objects, and the elements that cannot be reached can be judged as junk data.
  2. Clear: The garbage data is cleared.

Multiple execution of the mark-clear algorithm on a piece of memory results in a large number of discrete memory fragments. Too much fragmentation causes large objects to fail to allocate enough contiguous memory.

This is where you need to mark up.


  • Algorithm: Mark-compact algorithm
  • Principle:
  1. Tagging: As with mark-clean tagging, you start with a set of root elements, recursively traverse the set of root elements, and mark the reachable elements as active objects during this traversal.
  2. Defragment: Move all surviving objects to one end of memory
  3. Clear: Clears up memory outside the end boundary

Six full stop

Returns the directory

Since JavaScript is single-threaded, once the garbage collection algorithm is executed, the executing JavaScript script needs to be paused and resumed after the garbage collection is complete.

This behavior is called stop-the-world.

If there is a lot of data in the heap, it will take time to recycle and the page will lag. Therefore, to reduce this lag, V8 divides the tagging process into sub-tagging process, and the garbage collection markup and JavaScript application logic are alternated.

  • Optimization algorithm: Incremental Marking algorithm
  • Principle:
  1. In order to reduce the old generation of garbage collection caused by the backlog
  2. V8 breaks a complete garbage collection task into many smaller tasks
  3. Alternate garbage collection tags with JavaScript application logic

Vii References

Returns the directory

  • JavaScript Advanced – Memory mechanism (emoticons)

Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.