js - garbage collection mechanism

1. Why the system needs garbage collection

        Since strings, objects, and arrays have no fixed size, they can only be allocated dynamically when their size is known. Every time a JavaScript program creates a string, array, or object, the interpreter must allocate memory to store that entity. As long as memory is allocated dynamically like this, it must eventually be freed so that it can be reused, otherwise, the JavaScript interpreter will consume all available memory in the system and cause the system to crash.

        Unlike C/C++, JS has its own garbage collection mechanism (Garbage Collection). The JavaScript interpreter can detect when the program no longer uses an object. When he determines that an object is useless, he knows that the object is no longer needed and the memory it occupies can be released.

Second, the method of garbage collection

Mark clear, count references.

2.1 Mark removal

This is the most common garbage collection method. When a variable enters the environment, it is marked as "entering the environment". Logically speaking, the memory occupied by the variable entering the environment can never be released, and the memory occupied by the variable entering the environment can never be released. memory, as long as the execution process enters the corresponding environment, they may be used. When leaving an environment, it is marked as leaving the environment.

When the garbage collector is running, it will mark all the variables stored in memory (all are added), and then remove the variables in the environment variables and the variables referenced by the variables in the environment variables (conditional removal of marks) , delete all marked variables, the deleted variables cannot be accessed in the environment variables so they will be deleted, and finally the garbage collector completes the memory clearing work and reclaims the memory they occupy.

2.2 Reference counting method

Another less common method is the reference counting method. The reference counting method means the number of times each value is referenced. When a variable is declared and a value of a reference type is assigned to the variable, the number of references to this value On the contrary, if the variable containing the reference to this value obtains another value, the reference count of the original reference value will be reduced by 1. When the reference count of this value is 0, it means that there is no way to access it again This value, so the occupied memory is reclaimed, so that when the garbage collector runs again, it will release these values ​​with a reference count of 0.

Garbage collection example

For example:

var a="hello world"; var b="world";

var a=b;

//At this time, "hello world" will be released and the memory will be released

2.3 There will be memory leaks in the reference counting method

Here's why:

function problem() {

var objA = new Object(); var objB = new Object();

objA.someOtherObject = objB; objB.anotherObject = objA;

}

In this example, objA and objB refer to each other through their respective properties. In this case, the reference count of the two objects is 2. In the strategy of using reference counting, after the function is executed, both objects leave the scope , after the execution of the function is completed, because the count is not 0, if there are a large number of such mutual references, it will cause a memory leak.

Especially in DOM objects, it is also easy to have this kind of problem:

var element=document.getElementById(’‘); var myObj=new Object();

myObj.element=element; element.someObject=myObj;

This way there will be no garbage collection process.

Guess you like

Origin blog.csdn.net/qq_36384657/article/details/123263747