Why is garbage collection needed

Unlike C/C++, which has its own Garbage Collection mechanism, the js interpreter can detect when an object is no longer in use. When an object is determined to be useless, the Garbage Collection mechanism is activated. Due to the string in the js, objects, and the size of the array has no fixed, so when they already know the size of the time, to make them for dynamic storage allocation, js each create a string, array, object, the interpreter must allocate memory to take up the entity, and distribution of time, every time they create, distribution of the number of times much, The system runs out of memory and crashes game over!!

Methods of garbage recycling

Common:

Flag clearance count reference

Not common:

GC replication method Conservative GC generation recovery incremental GC tri-color labeling method

Here are two common garbage collection mechanisms

Garbage collection method a marked cleanup

Tag cleanup is the most common garbage collection mechanism. When a variable enters the environment, it is marked as “in”. Logically, variables marked as “in” are never released (cleared). The garbage collector marks all variables stored in memory at runtime. Variables in environment variables are not affected, and variables referenced by environment variables are safe (conditional de-marking). However, any variables marked “out of environment” are deleted, clearing their memory usage

The name of the mark clearing algorithm can be divided into two parts: mark and sweep. This algorithm can be divided into two stages, one is the marking stage, one is the clearing stage, the following are respectively introduced. 1). Mark phase: In this phase, the garbage collector iterates from the Mutator (application) root object. An identity is added to each object that can be accessed from the root object, and the object is identified as reachable. 2). Remove stage: at this stage, the garbage collector to heap memory linear traverse from the beginning to the end, if find any object has not been identified as can be reached, then the object memory recovery, and clear the originally marked as to the object identification, so that the next garbage collection operations

Js implementations of IE, Firefox, Opera, Chrome, and Safari all use a marked sweep garbage collection strategy or similar strategies, with different garbage collection intervals

Garbage collection mechanism two counts references

Counting references means counting the number of times each value is not referenced

When we declare a variable and assign the value of a reference type to the variable, we calculate that the number of references to the value is 1; If the value of this reference type is later acquired by another value, then the count is reduced by one to zero; When we declare a variable reference value of 0. This variable is no longer useful, so the next time the garbage collector runs, it reclaims the value with reference to 0.

The language engine has a “reference table” that holds the number of references to all resources (usually values) in memory. If the number of references to a value is zero, the value is no longer needed, so it can be freed.

What are the problems with counting references?

It says to the reference value of the variable to 0, counting can produce garbage collection reference So at the time of two function reference each other, effect after leaving the scope, as variables are useless and should be recycled, but due to the mutual reference, its reference value and not become zero, so will not be recycled, Memory leaks can occur if there are too many references in your code. When using a function reference or closure, manually free the function.