On the automatic garbage collection mechanism of JavaScript _javascript skills

Source: Internet
Author: User
Tags garbage collection object model time interval

The execution environment is responsible for managing the memory used during code execution, when writing JavaScript programs, the allocation of required memory, and the recovery of unwanted memory fully implemented automatically.

Principle:

Identify the variables that will not continue to be used, and then release the memory they occupy. To do this, the garbage collector periodically performs this operation at a fixed time interval (or a predetermined collection time in code execution).

Garbage collection in the way:

1. Mark Clear (Mark-and-sweep)

The most commonly used method of garbage collection. When a variable enters the environment, it marks the variable as "entering the environment." Logically, the memory occupied by variables entering the environment can never be released, because they may be used as long as the execution stream enters the appropriate environment. When a variable leaves the environment, it is marked as "away from the environment."

The collector immediately marks all variables stored in memory at run time, and then it removes the variables in the environment and the tokens of the variables referenced by the variables in the environment. The rest of the tagged variables will be considered variables to be deleted because the variables in the environment are already inaccessible to them. Finally, the garbage collector completes the memory cleanup, destroys those tagged values, and reclaims the memory space they occupy.

2. Reference count (reference counting)

The trace records the number of times each value is referenced, and when the value is referred to 0 o'clock, there is no way to access the value again, so that the memory space it occupies can be recycled back. When the garbage collector is run again, the memory that is occupied by values that have a reference number of zero is freed.

(1) There is a problem in the application mechanism: circular reference.

A circular reference means that object A contains a pointer to object B, and object B also contains a reference to object A. (so that their references may never be 0, and if the function is repeatedly called repeatedly, it can cause a lot of memory to be recycled.) )

(2) in IE, the BOM and the objects in the DOM use C + + in the form of COM (Component Object model, Component object models) objects, while the COM object's garbage collection mechanism uses the reference counting strategy.

The following is a problem with circular references caused by COM objects:

eg

var Element=document.getelementbyid ("Some_element");
 var myobject=new Object ();
 myobject.element=element;
 Element.someobject=myobject;

The example above creates a circular reference between a DOM element and a native JavaScript object (myObject). Because of this circular reference, even if the DOM in the example is removed from the page, it will never be reclaimed.

You can manually disconnect the links between native JavaScript and DOM elements by using the following code:

Myobject.element=null;
Element.someobject=null;

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.