A brief analysis of JavaScript garbage collection mechanism

Source: Internet
Author: User

The JavaScript language is an excellent scripting language. This includes the flexibility of scripting languages and features of many high-level languages. For example, to build and instantiate an object, the garbage collection mechanism (Gc:garbage collecation). Usually we use new to create objects, and the GC is responsible for reclaiming objects that occupy memory areas. Therefore, understanding the GC can deepen your understanding of the JavaScript garbage collection mechanism. Huimin County House Zhe Clothing

When the GC reclaims memory, it first determines whether the object is referenced by another object. The memory area of the object is freed if no other object reference is determined. So how to determine if an object is no longer being referenced is the key to GC.

<script type= "Text/javascript" >function aa () {this.rr = "Pop window";    } function bb () {this.rr = "popup Window";} var b1;function cc () {var a1 = new AA (); b1 = new BB (); return B1;} CC (); alert (B1.RR) </script>

As in the code above, after executing cc () A1 is recycled, we can then pop the text window through B1.RR. In some basic books it is explained that A1 is a local variable and B1 is a global variable. When a local variable is executed, it is reclaimed by GC. But not all, the following code:

<script type= "Text/javascript" >function aa () {this.rr = "Pop window";    } function bb () {this.rr = "popup Window";} function cc () {var a1 = new AA (), var b1 = new BB (); return B1;} var B1 = cc (); alert (B1.RR);</script>

At this point the a1,b1 in the CC function are local variables, but the text window will still pop up. Description B1 is not recycled by GC. Therefore, local variables in JavaScript are not collected at all times by GC.

The GC recovery mechanism also needs to be understood in a closer step. Several concepts are introduced at this time: doubly linked list, scope chain, active object (for ease of understanding, simplified the concept of the original, in which the two-way list describes the complex objects of the upper and lower levels of the relationship. The scope chain and the active object are a node in a doubly linked list, respectively. In the case of function cc, the variable-level relationship is:

Window<=>cc<=>a1<=>rr<=>b1<=>rr

When executing the CC () method, references to variables in memory are interpreted as follows:

    1. CC's active objects include A1 and B1, whose scope chain is window
    2. CC's active objects include A1 and B1, whose scope chain is window
    3. The A1 Activity object includes the RR, whose scope chain is CC
    4. The B1 activity object includes the RR, whose scope chain is CC

When you execute CC (), the CC's execution environment creates an active object and a scope chain. Its local variable a1,b1 will be hung in the CC's active object. When CC () finishes executing, the execution environment attempts to reclaim the memory occupied by the active object. However, due to the local variable B1 through the return B1, it adds a scope chain: WINDOW<=>B1<=>RR, so the GC stops the B1 collection. So if you want to promote a local variable/function to a global one, adding a scope chain to it will be OK.

Also, it becomes important to control the scope chain of the object as well. The GC cannot reclaim the target object because of the scope chain. For example:

<script language= "JavaScript" ><!--//Cat function Cat (name) {var zhuren; this.name = name;        Set Master This.addzhuren = function (ZR) {zhuren = ZR;} This.getzhuren = function () {return zhuren;}}    Master function Zhuren (name) {this.name = name;} Create owner: var ZR = new Zhuren ("Zhangsan");//create cat var cat1 = new Cat ("Asan");//Set the cat's Master Cat1.addzhuren (ZR);//release Master ZR = null; There is a reference to the Host object Alert (Cat1.getzhuren (). Name)//--></script>

A brief analysis of JavaScript garbage collection mechanism

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.