JavaScript memory recovery mechanism understanding _JAVASCRIPT Skills

Source: Internet
Author: User
Tags garbage collection
1. Nagging
The JavaScript language is an excellent scripting language. The flexibility to include scripting languages also features a number of advanced languages. For example, a garbage collection mechanism (Gc:garbage collecation) is used to build and instantiate an object. Usually we use new to create objects, The GC is responsible for reclaiming the memory area of the object. Therefore, understanding the GC can deepen the understanding of the JavaScript garbage collection mechanism.
2. Interpreting GC with local variables and global variables
When a GC reclaims memory, it first determines whether the object is referenced by another object. Frees the object memory area when it is determined that no other object reference is available. So how to determine that an object is no longer referenced is the key to GC.
Copy Code code as follows:

<script>
function aa () {
THIS.RR = "Bomb window";
}
function bb () {
THIS.RR = "Bomb 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 the CC () is A1, we can eject the text window through B1.RR. In some basic books, it is explained as follows: A1 is a local variable, and B1 is a global variable. The local variable is collected after it has been executed. But not all of this, the following code:
Copy Code code as follows:

<script>
function aa () {
THIS.RR = "Bomb window";
}
function bb () {
THIS.RR = "Bomb 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 is a local variable, but the text window still pops up. Indicates that the B1 is not collected by GC. Therefore, local variables in JavaScript are not collected by GC at all times.
3. Abstract Understanding GC
The GC recovery mechanism also needs to be understood a step closer. At this point, several concepts are introduced: bidirectional linked list, scope chain, active object (for the sake of understanding, simplifies the concept of the original text [http://softbbs.pconline.com.cn/9497825.html]), in which the bidirectional linked list describes the upper and lower level relationships of complex objects. The scope chain is a node in a two-way list, respectively, with the active object. Take function cc as an example variable hierarchy relations are:
Window<=>cc<=>a1<=>rr
<=>b1<=>rr
In the case of the CC () method, the reference relationship of the variable in memory, as shown above, is explained in the following text:
Window's active object includes cc, assuming window is the top-level object (because it is not recycled in operation)
The active objects of CC include A1 and B1, whose scope chain is window
The active object of A1 includes RR, whose scope chain is CC
The active object of B1 includes RR, whose scope chain is CC
When performing CC (), the execution environment of the CC creates an active object and a scope chain. Its local variable a1,b1 will hang in the active object of CC. When the CC () is finished, the execution environment attempts to reclaim the memory occupied by the active object. But because local variables B1 through return B1, Adds a scope chain to it: WINDOW&LT;=&GT;B1&LT;=&GT;RR, so the GC stops B1 recycling.
So if you want to promote a local variable/function to global, it's OK to add a scope chain to it.
At the same time, it becomes important to control the scope chain of the object. Because the scope chain accidentally causes the GC to not reclaim the target object. For example:
Copy Code code as follows:

<script language= "JavaScript" >
<!--
//cat
Function CA T (name) {
var zhuren;
THIS.name = name;
//Set master
This.addzhuren = function (ZR) {
Zhuren = ZR;
}
This.getzhuren = function () {
return zhuren;
}
}
//host
Function Zhuren (name) {
THIS.name = name;
}
//Create host:
var ZR = new Zhuren ("Zhangsan");
//Create cat
var cat1 = new Cat ("Asan");
//Set the owner of the cat
Cat1.addzhuren (ZR);
//Release master
ZR = null;
//There is also a reference to the host object
Alert (Cat1.getzhuren (). 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.