Javascript is an excellent scripting language. It includes the flexibility of the scripting language and features of many advanced languages. For example, it allows you to build and instantiate an object
Collection Mechanism (GC: Garbage
Collecation). Normally we use new to create an object, and GC is responsible for collecting the memory occupied by the object. Therefore, we know GC, which can deepen the collection of Javascript garbage.
Mechanism understanding.
1. Use local variables and global variables to explain GC
When GC recycles the memory, it first determines whether the object is referenced by other objects. If no other object is referenced, the memory area of the object will be released. Therefore, how can we determine whether the object is no longer referenced?
The key to GC.
-
- <SCRIPT>
-
- FunctionAA (){
- This. RR ="Pop-up window";
-
- }
-
- FunctionBB (){
-
- This. RR ="Pop-up window";
-
- }
-
-
- VaRB1;
-
- FunctionCC (){
-
- VaRA1 =NewAA ();
-
- B1 =NewBB ();
- ReturnB1;
-
- }
-
-
-
- CC ();
-
- Alert (b1.rr)
-
- </SCRIPT>
AboveCodeAfter CC () is executed, A1 is recycled. 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. local
After the variable is executed, it will be recycled by GC. But this is not all the case. The following code:
-
- <SCRIPT>
-
- FunctionAA (){
- This. RR ="Pop-up window";
-
- }
-
- FunctionBB (){
-
- This. RR ="Pop-up window";
-
- }
-
-
- FunctionCC (){
-
- VaRA1 =NewAA ();
-
- VaRB1 =NewBB ();
-
- ReturnB1;
- }
-
-
-
- VaRB1 = Cc ();
-
- Alert (b1.rr );
-
- </SCRIPT>
In this case
Both A1 and B1 are local variables, 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.
2. 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 (to facilitate understanding, the concept of the original article is simplified.
[Http://softbbs.pconline.com.cn/9497825.html]). A two-way linked list describes the upper and lower levels of complex objects.
The scope chain and activity object are two-way linked lists.
Take the function cc as an example. The hierarchical relationship of variables is as follows:
- 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. when CC () is executed, the execution environment
Will try to recycle the memory occupied by the active object, but because the local variable B1 uses return
B1, adds a scope chain for it: Window <=> B1 <=> RR, so 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:
- <Script language ="JavaScript">
-
- <! --
-
- // Cat
-
- FunctionCAT (name ){
-
- VaRZhuren;
-
- This. Name = Name;
-
- // Set the master
-
- This. Addzhuren =Function(Zr ){
-
- Zhuren = Zr;
-
- }
-
-
-
- This. Getzhuren =Function(){
- ReturnZhuren;
-
- }
-
- }
-
-
- // Master
-
- FunctionZhuren (name ){
-
- This. Name = Name;
-
- }
-
-
- // Create a master:
-
- VaRZR =NewZhuren ("Zhangsan");
-
- // Create a cat
- VaRCat1 =NewCAT ("Province");
-
- // Set the host of the cat
-
- Cat1.addzhuren (Zr );
-
- // Release the master
-
- ZR =Null;
- // There is still a reference to the master object.
-
- Alert (cat1.getzhuren (). Name)
-
- // -->
-
- </SCRIPT>