This document has been updated to Github.

accessibility

Reachability means that a value in memory is accessed in some way and that value is kept in memory for a long time.

Such as:

var obj = {
  name: 'Joe'.age: 12.other: {
    height: 180}}Copy the code

As shown above, chang SAN, 12, 180 and even their objects are reachable because they are all referenced by obj and its descendant variables (name, age, other, height), and other variables are accessible through obj and its descendant variables. We call these reachable objects (also known as active objects).

If we reassign obj to null, then obj and {name: ‘John ‘, age: 12, other: {height: 180}} is disconnected, and other variables cannot be accessed through obj, so they are called unreachable objects (also called inactive objects), even though they refer to each other (180 is referenced by height, height is referenced by other).

For more information about accessibility, see garbage collection

Intrinsic reachable value

  • Global local variables and parameters
  • Currently nested variables and arguments of other functions on the call chain
  • The global variable
  • There are some other ones, internal ones

The values produced by the above situation are called inherently reachable values, also known as roots.

The first and third points are easy to understand, because the browser’s global variable is simply called window, which can be called anywhere. We consider window reachable, so are its internal variables and functions.

If we create a function in the console, it is automatically mounted to the window variable, so we call sum() in the same way as sum().

Understanding of the second point:

function sum (a, b) {
  return function (c) {
    returna + b + c; }}var a = sum(1.2);
console.log(a(3)); / / output 6
Copy the code

Since sum is mounted on the window, its parameters and variables are reachable. Variable C is referenced by a + b + C, so c is also considered reachable.

The above understanding is personal understanding, if there is any deviation, please correct, for feeling.

Stack and heap

Memory contains heap memory and stack memory. In JS, values of reference types (object, array, new Date, etc.) are put into heap memory, while values of non-reference types (string, number, etc.) are put into stack memory.

For a more detailed understanding of stacks and heaps, see understanding JS Stacks

According to the characteristics of heap memory and stack memory, different reclamation methods and strategies are adopted.

Stack recovery

Stack memory reclamation is relatively simple: the JS engine destroys the execution context stored in the stack space by moving the ESP pointer (stack pointer) down. Remember that this is the execution context, or (execution context).

var a = 1;
function foo() {
  var b = 2;
  var c = 1001;
};

// Function call
foo();
Copy the code

The stack is like putting rocks in a bucket. The latest rocks are called the global context, and they are not cleaned up unless the user changes the page. When a function is called, an execution context is created, which is automatically cleaned up after execution, unless closure is generated, which is the second point in inherently reachable values.

The execution context in the stack is reclaimed through the ESP pointer.

Recovery (GC) process:

1. When the page is entered for the first time, the global context is generated and entered into the stack, and the ESP pointer points to this environment; 2. When foo is called, the execution context of foo is merged into the stack, with ESP pointing to foo. 3. When foo completes, ESP adjusts downward to the global context. 4. When another function executes, this function is pushed onto the stack, overwriting foo’s execution context and pointing the ESP pointer to it. 5. After the execution, adjust it downward again to point to the global context; 6. The global context is destroyed when the page is closed.

The specific process is shown in the figure below:

Pile of recycling

The heap collection process is different from the stack collection process. Instead of cleaning through the ESP pointer, the heap collection process is marked – clean – memory cleaning process.

Since the life cycle of most objects is very short and that of a small number of objects is very long, V8 divides the heap space into new generation space and old generation space.

Cenozoic spatial volume is small, usually 1-8m, not completely fixed and allocated according to the situation. The old generation space is large and usually holds frequently called or large values.

According to the characteristics of the two, the recycling method is also different, but the major process is still marked – clean – memory arrangement process.

Cenozoic recovery

The Space of the new-generation memory is divided into Eden region and Survivor region, and Survivor region is divided into From Space and To Space.

Recovery (GC) process:

1. When an object is generated, it will enter the Eden area of the new generation (large objects will directly enter the old generation); 2. Mark the object as active if it is still referenced after an up-down execution environment is complete; otherwise, mark it as inactive; 3. When the memory in Eden area runs out, move the active objects to From Space in the order they entered, and the remaining objects in Eden area are all inactive objects; 4. Clear all objects in Eden; 5. Turn over Survivor area and Eden area as a whole; 6. New objects are added continuously. When Eden area memory runs out, repeat steps 2, 3, 4 and 5. 7. Objects that survived twice in CG are considered To have a long life cycle and put into old generation Space from To Space.

The above strategy is called the object promotion strategy, the algorithm is called the Scavenge algorithm, and the new-generation algorithm does not have a demining phase because the object enters a Survivor zone in an orderly manner, so there are no faults (fragments).

The specific process is shown as follows:

Old generation recycling

The old generation recycling mechanism is simpler than the new generation recycling mechanism, and adopts mark-clearance method. That is, after the execution of an up-down execution environment, if the object is still referenced, then mark it as an active object, otherwise it is inactive. When the memory is exhausted, the inactive object is cleared, and all active objects are reordered and fragmented.

reference

This is not to say that the browser garbage collection mechanism only uses the algorithms and strategies described above, but there are many others, just that the above processes are the main ones.

The above content is sorted out according to the relevant references. If there is any deviation, please correct it.

Garbage Collection V8 Journey: Garbage collector


For more documentation, see:

Online address [Front-end Tangerine]

GitHub Warehouse