Talking about garbage collection mechanism in JavaScript

  • 2021-08-21 19:38:29
  • OfStack

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

When writing JavaScript programs, developers no longer need to care about memory usage, and the allocation of required memory and the recovery of useless memory are completely automatically managed.

The principle of this garbage collection mechanism is actually very simple: find those variables that are no longer used, and then release the memory occupied by them. To do this, the garbage collector periodically performs this 1 operation at regular intervals (or at a predetermined collection time during code execution).

Specific to the implementation in the browser, there are usually two strategies, namely, tag clearing and reference counting.

1. Mark clear

The most common garbage collection method in JavaScript is tag cleanup (mark-and-sweep). When a variable enters the environment (for example, declaring a variable in a function), the variable is marked as "entering the environment". When a variable leaves the environment, it is marked as "leaving the environment".

When the garbage collector runs, it tags all variables stored in memory. You can use any notation, for example, by flipping a particular bit to record when a variable entered the environment, or by using a list of variables that "entered the environment" and a list of variables that "left the environment" to track which variable changed.

Then, it removes the markup of variables in the environment and variables referenced by variables in the environment. Variables that are tagged later are considered to be ready for deletion because the variables in the environment are no longer accessible.

Finally, the garbage collector completes the memory cleanup, destroying those marked values and reclaiming the memory space they occupy.

2. Reference Count

Another less common garbage collection strategy is called reference counting (reference counting). Reference count means tracking the number of 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 that value is 1. If the same value is assigned to another variable, the number of references to the value is increased by 1. Conversely, if a variable containing a reference to this value takes another value, the number of references to that value is minus 1.

When the number of references to this value becomes 0, it means that there is no way to access this value again, so the memory space occupied by it can be recovered.

This way, the next time the garbage collector runs again, it will free up the memory occupied by those values with zero references.

Problem: Whenever COM (Component Object Model, component object model) objects are involved in IE, there will be a circular reference problem. As shown in the following code:


var element = document.getElementById("some_element");
var myObject = new Object();
myObject.element = element;
element.someObject = myObject;

This example creates a circular reference between an DOM element (element) and a native JavaScript object (myObject).

The variable myObject has a property named element that points to the element object.

The variable element also has an attribute named someObject that refers to myObject.

Because of this circular reference, even if the DOM in the example is removed from the page, it will never be recycled.

Solution: It is best to manually disconnect native JavaScript objects from DOM elements when they are not in use.


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

Setting a variable to null means disconnecting the variable from the value it previously referenced. The next time the garbage collector runs, it will delete these values and reclaim the memory they occupy.

3. Manage memory

Ensuring that you use the least memory can make your page get better performance. The best way to optimize memory footprint is to save only the necessary data for executing code.

1 Once the data is no longer useful, it is best to release its reference by setting its value to null-a practice called dereference (dereferencing).

This 1 approach applies to most global variables and properties of global objects. Local variables are automatically dereferenced when they leave the execution environment, as shown in the following example:


function createPerson(name){
 var localPerson = new Object();
 localPerson.name = name;
 }
var globalPerson = createPerson("Nicholas");
globalPerson = null; //  Manual release globalPerson  Reference to 

The variable globalPerson takes the value returned by the createPerson () function. Inside the createPerson () function, we create an object and assign it to the local variable localPerson, and then add a property named name to the object. Finally, when this function is called, localPerson is returned as a function value and assigned to the global variable globalPerson.

Since localPerson leaves its execution environment after the createPerson () function is executed, we do not need to explicitly dereference it.

However, for the global variable globalPerson, we need to dereference it manually when we are not using it, which is the purpose of the last line of code in the above example.

The above is to talk about JavaScript garbage collection mechanism in detail, more information about JavaScript garbage collection please pay attention to other related articles on this site!


Related articles: