Analysis on the garbage collection mechanism of JavaScript

Source: Internet
Author: User

JavaScript is an excellent scripting language. This includes the flexibility of the scripting language, as well as the features of many advanced languages. For example, a Garbage collection mechanism (GC: Garbage Collecation) is used to create and instantiate an object ). Normally, we use new to create an object. GC is responsible for reclaiming the memory occupied by the object. Therefore, understanding GC can deepen understanding of the javascript garbage collection mechanism.

When GC recycles memory, it first determines whether the object is referenced by other objects. Release the memory area of the object if no other object is referenced. Therefore, determining that the object is no longer referenced is the key to GC.

<Script type = "text/javascript"> function aa () {this. rr = "pop-up window";} function bb () {this. rr = "pop-up window";} var b1; function cc () {var a1 = new aa (); b1 = new bb (); return b1;} cc (); alert (b1.rr) </script>

In the above Code, a1 is recycled after cc () is executed. Then we can use b1.rr to pop up the text window. In some basic books, a1 is a local variable, and b1 is a global variable. After the local variable is executed, it will be recycled by GC. But this is not the case. The following code:

<Script type = "text/javascript"> function aa () {this. rr = "pop-up window";} function bb () {this. rr = "pop-up window";} function cc () {var a1 = new aa (); var b1 = new bb (); return b1;} var b1 = cc (); alert (b1.rr); </script>

In this case, both a1 and b1 are local variables in the cc function, but a text window is displayed. It indicates that b1 is not recycled by GC. Therefore, not all local variables in javascript are recycled by GC.

The GC recovery mechanism needs to be further understood. Several concepts are introduced at this time: two-way linked list, scope chain, and activity object (to facilitate understanding, the original concept is simplified. The two-way linked list describes the upper and lower levels of complex objects. The scope chain and activity object are a node in the two-way linked list respectively. Take the function cc as an example. The hierarchical relationship of variables is:

window<=>cc<=>a1<=>rr<=>b1<=>rr

When executing the cc () method, the reference of variables in the memory is as follows:

  1. The cc activity objects include a1 and b1, And the scope chain is window.
  2. The cc activity objects include a1 and b1, And the scope chain is window.
  3. A1 Includes rr, and its scope chain is cc.
  4. Activity objects of b1 include rr, And the scope chain is cc.

When you execute cc (), the cc execution environment creates an activity object and a scope chain. The local variables a1 and b1 are all mounted to the cc activity object. After cc () is executed, the execution environment will try to recycle the memory occupied by the active object. However, because the local variable b1 adds a scope chain for it through return b1: window <=> b1 <=> rr, GC stops recycling b1. Therefore, if you want to promote a local variable/function to a global one, it is OK to add a scope chain to it.

At the same time, it is also important to control the scope chain of objects. GC cannot recycle the target object because the scope chain will accidentally. For example:

<Script language = "JavaScript"> <! -- // Cat function cat (name) {var zhuren; this. name = name; // set the master this. addZhuRen = function (zr) {zhuren = zr;} this. getZhuRen = function () {return zhuren ;}// master function zhuren (name) {this. name = name;} // create a host: var zr = new zhuren ("zhangsan"); // create a cat var cat1 = new cat ("Courier "); // set the cat's host cat1.addZhuRen (zr); // release the host zr = null; // The reference alert (cat1.getZhuRen () to the Host object also exists here (). name) // --> </SCRIPT>

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.