About the JavaScript garbage collection mechanism

Source: Internet
Author: User

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

The garbage collector must track which variable is useful and Mark variables that are no longer useful for future recovery. The policies used to identify useless variables vary with implementations. However, there are usually two specific implementations in the browser.

1. mark and clear

The most common method of garbage collection in JavaScript is mark-and-sweep ). When a variable enters the environment, it is marked as "entering the environment ". Logically, the memory occupied by variables entering the environment cannot be released, because they may be used as long as the execution stream enters the corresponding environment. When a variable leaves the environment, it is marked as "leaving the environment ".

When running, the garbage collector adds a flag to all variables stored in the memory (of course, any flag method can be used ). Then, it removes the environment variables and the variables referenced by the environment variables. The variables marked after this are considered as the variables to be deleted, because they are no longer accessible to environment variables. Finally, the garbage collector clears the memory, destroys the marked values, and recycles the memory space they occupy.

2. Reference count
Another uncommon garbage collection policy is reference counting ). The reference count indicates 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 times this value is referenced is 1. if the same value is assigned to another variable, the number of times the value is referenced increases by 1 ., Conversely, if another value is obtained for the referenced variable, the number of references of this value is reduced by 1. when the number of references for this value changes to 0, it indicates that there is no way to access this value again, so the occupied memory space can be reclaimed. In this way, when the Garbage Collector runs again next time, it will release the memory occupied by the value with zero references.

Circular reference: Object A contains A pointer to object B, and object B also contains A reference to object. As follows:

Function problem (){
Var objectA = new Object ();
Var objectB = new Object ();

ObjectA. someOtherObject = objectB;
ObjectB. anotherObject = objectA;
}

In this example, objectA and objectB are referenced by their respective attributes. That is to say, both objects are referenced twice. in the implementation of the Flag clearing policy, since the two objects are out of the scope after the function is executed, this mutual reference is not a problem. However, in the implementation of the Reference counting policy, after the function is executed, objectA and objectB will continue to exist, so the number of their references will never be 0. If this function is repeatedly referenced, a large amount of memory will not be recycled.

In addition, some objects in IE are not native JavaScript objects. For example, the BOM and DOM objects are implemented in the form of COM (Component Object Model, Component Object Model) objects. The reference counting policy adopted by the garbage collection mechanism of COM objects. Therefore, even if the JavaScript engine of IE is implemented using the mark clearing policy, the COM Object accessed by JavaScript is still implemented based on the reference counting policy.

That is to say, as long as IE involves COM objects, there will be a circular reference problem. As follows:

Var element = document. getElementById ("some_element ");
Var myObject = new Object ();
MyObject. element = element;
Element. someObject = myObject;

In this example, a circular reference is created between a DOM element and a native JavaScript Object. The variable myObject has an attribute named element pointing to the element object, and vice versa. Because of this circular reference, even if the DOM in the example is removed from the page, it will never be recycled.

To avoid loop references like this, it is best to manually disconnect native JavaScript objects from DOM elements without using them.

MyObject. element = null;
Element. someObject = null;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.