Javascript Memory collection mechanism comprehension _ javascript skills

Source: Internet
Author: User
Javascript is an excellent scripting language. It includes the flexibility of the scripting language and features of many advanced languages. 1. Nagging
Javascript is an excellent scripting language. in addition to the flexibility of the scripting language, there are also many advanced language features. 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.
2. Use local variables and global variables to explain GC
When GC recycles the memory, it first checks whether the object is referenced by other objects. the memory area of the object will be released if no other object is referenced. therefore, determining that the object is no longer referenced is the key to GC.

The Code is as follows:


Script
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 a 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 not all. The following code:

The Code is as follows:


Script
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


At this time, both a1 and b1 are local variables in the cc function, but a text window is displayed, indicating that b1 is not recycled by GC. Therefore, not all local variables in javascript are recycled by GC.
3. abstract and understand 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 (for ease of understanding, the concept of the original article is simplified [http://softbbs.pconline.com.cn/9497825.html]). The two-way linked list describes the upper and lower. 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
(The original article has a detailed explanation) when executing the cc () method, the reference relationships of variables in the memory are as follows:
Window activity objects include cc. Suppose window is a top-level object (because it will not be recycled during running)
The cc activity objects include a1 and b1, And the scope chain is window.
A1 Includes rr, and its scope chain is cc.
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 becomes important to control the scope chain of objects. GC cannot recycle the target object because the scope chain will accidentally. For example:

The Code is as follows:


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.