


Understanding javascript memory recycling mechanism_javascript skills
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
