JavaScript has automatic garbage collection, and the execution environment is responsible for managing the memory used during code execution.

The garbage collection mechanism has two ways of marking cleanup and reference cleanup.

How garbage collection works

Find variables that are no longer in use and free up memory. To do this, the garbage collector performs this operation periodically at fixed intervals (or at predetermined collection times during code execution).

The normal life cycle of a variable

Local variables exist only during the execution of a function.

In this process, local variables are allocated space in stack (or heap) memory to store their values. These variables are then used in the function until the end of the function execution.

At this point, there is no need for local variables to exist, so they can be freed up for future use.

In this case, it is easy to determine whether the variable is still necessary; But not all cases are so easy to judge.

The garbage collector must keep track of which variables are useful and which are not, and any variables that are not are marked so that they can be reclaimed in the future.

The policies used to identify useless variables may vary from implementation to implementation, and there are usually two.

Mark clearing – Often used

When a variable is entered into the environment (such as declaring a variable in a function), it is marked as “entered into the environment.”

When a variable leaves the environment, it is marked as “out of the environment.”

At run time, the garbage collector marks all variables stored in memory (it can be marked in any way).

It then unflags variables in the environment and those referenced by variables in the environment.

Variables tagged after this point are considered to be ready for deletion because they are no longer accessible to variables in the environment.

Finally, the garbage collector completes the memory cleanup. Destroy the tagged values and reclaim the memory they occupy.

Reference counting – not often used

Track how many times each value is referenced.

When a variable is declared and a reference type value is assigned to the variable, the number of references to the value is 1.

If the same value is assigned to another variable, the number of references to the value is increased by one.

Conversely, if a variable containing a reference to this value is assigned another value, the number of references to that value is reduced by one.

When the number of references to the value becomes zero, there is no way to access the value, so the memory occupied by the value can be reclaimed.

The next time the garbage collector runs again, it frees the memory occupied by values with zero references.

Problems with reference counting

This algorithm can cause problems when accessing non-native JavaScript objects (such as DOM elements) in IE. The “reference counting” algorithm can cause problems when there are circular references in your code.

var element = document.getElementById('box');
var myObject = new Object(a); myObject.element = element; element.someObject = myObject;Copy the code

In this example, there is a circular reference, and even if the DOM element is removed from the page, it is not recycled.

It needs to be cleared manually, preferably by setting its value to null to release its reference – a practice called dereferencing.

myObject.element = null; element.someObject = null;

Dereferencing variables not only helps eliminate circular references, but also benefits garbage collection.

To ensure efficient memory reclamation, global objects, global object properties, and circular reference variables that are no longer used should be dereferenced in a timely manner.