JavascriptWe have our own garbage collection mechanism, which saves us the trouble of manual memory collection.
Here we will briefly record the content about this part.
JavascriptThere are two garbage collection policies
I,Mark Clear
This method sets all the variables in the memory to BITs during garbage collection, removes the variables referenced in the execution environment and execution environment, and revokes the remaining variables as useless variables.
Note:
Currently, most browsers use the following methods:ProgramSet unused variablesNullThe garbage collector can immediately release the corresponding memory in the next execution.
II,Reference count
Is to track the number of variables referenced, when the number of times of reference is0When the variable is no longer used, the memory is reclaimed.
Note:
The memory cannot be released due to the loop reference problem. If the loop size is large, a large amount of memory will be wasted. For example:
Function Test ()
{
VaR O1 = new object ();
VaR O2 = new object ();
O1.obj = O2;
O2.obj = O1;
}
Such as program O1, O2 All 2 , The function will not be reduced 0 So the memory cannot be released.
Solution:
You can manually remove the circular references.
O1.obj = o2.obj = NULL;