1. Nagging
The JavaScript language is an excellent scripting language. In addition to the flexibility of a scripting language, it also has many features of a high-level language. For example, it allows the construction and instantiation of an object, garbage Recycling mechanism (GC: Garbage Collection). Usually we use new to create objects, and GC is responsible for recycling the memory area occupied by the object. Therefore, understanding GC can deepen your understanding of the JavaScript garbage collection mechanism.
2. Use local variables and global variables to explain GC When GC reclaims memory, it will first determine whether the object is referenced by other objects. After determining that there is no other object reference, the object memory will be released Area. So how to determine that the object is no longer referenced is the key to GC.
<script> <br>function aa(){ <br>this.rr = "Pop-up window"; <br>} <br>function bb(){ <br>this.rr = "Pop-up window" "; <br>} <br>var b1; <br>function cc(){ <br>var a1 = new aa(); <br>b1 = new bb(); <br>return b1; <br> } <br>cc(); <br>alert(b1.rr) <br></script>
In the above code, a1 is recycled after executing cc(). We can pop up a text window through b1.rr. In some basic books, it is explained that a1 is a local variable and b1 is a global variable. The local variables will be recycled by the GC after execution. But this is not always the case, the following code:
<script> <br>function aa(){ <br>this .rr = "pop-up window"; <br>} <br>function bb(){ <br>this.rr = "pop-up window"; <br>} <br>function cc(){ <br>var a1 = new aa(); <br>var b1 = new bb(); <br>return b1; <br>} <br>var b1 = cc(); <br>alert(b1.rr); <br>< ;/script> <br>
</div> <br>At this time, a1 and b1 in the cc function are local variables, but a text window will still pop up. This means that b1 has not been recycled by GC. Therefore, local variables in javascript are not always All are recycled by GC. <br><strong>3. Abstract understanding of GC <br></strong>The GC recycling mechanism requires further understanding. At this time, several concepts are introduced: doubly linked lists, scope chains, and active objects (for the convenience of understanding, the concepts in the original text are simplified [http://softbbs.pconline.com.cn/9497825.html]), in which doubly linked lists are described The upper and lower hierarchical relationship of complex objects. The scope chain and the active object are respectively a node in the doubly linked list. Taking the function cc as an example, the variable hierarchical relationship is: <br>window<=>cc<=>a1<=> ;rr <br><=>b1<=>rr <br>(Detailed explanation in the original text) When executing the cc() method, the reference relationship of the variables in the memory is as shown above, and the text explanation is as follows: <br>window The active objects of cc include cc, assuming that window is a top-level object (because it will not be recycled during operation) <br>The active objects of cc include a1 and b1, and their scope chain is window <br>The active objects of a1 include rr, whose role The domain chain is cc <br> The active object of b1 includes rr, and its scope chain is cc <br> When cc() is executed, the execution environment of cc will create an active object and a scope chain. Its local variables a1, b1 will be hung in the active object of cc. When cc() is executed, the execution environment will try to reclaim the memory occupied by the active object. However, because the local variable b1 returns b1, a scope chain is added to it: window<=> ;b1<=>rr, so GC stops recycling b1. <br>So if you want to promote a local variable/function to global, just add a scope chain to it. <br>At the same time, it is also important to control the scope chain of the object. The scope chain will accidentally cause the GC to be unable to recycle the target object. For example: <br><div class="codetitle">
<span><a style="CURSOR: pointer" data="27440" class="copybut" id="copybut27440" onclick="doCopy('code27440')"><u>Copy code</u></a></span> The code is as follows: </div>
<div class="codebody" id="code27440"> <br><SCRIPT LANGUAGE="JavaScript"> <br><!-- <BR>//cat<BR>function cat( name){ <BR>var zhuren; <BR>this.name = name; <BR>//Set the owner<BR>this.addZhuRen = function(zr){ <BR>zhuren = zr; <BR>} <BR>this.getZhuRen = function(){ <BR>return zhuren; <BR>} <BR>} <BR>//Master<BR>function zhuren(name){ <BR>this.name = name; <BR> } <BR>//Create owner: <BR>var zr = new zhuren("zhangsan"); <BR>//Create cat<BR>var cat1 = new cat("asan"); <BR>//Set up The owner of the cat<BR>cat1.addZhuRen(zr); <BR>//Release the owner<BR>zr = null; <BR>//There is also a reference to the owner object here<BR>alert(cat1.getZhuRen ().name) <BR>//--> <br></SCRIPT>