In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "understanding the garbage collection mechanism of JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the understanding of JavaScript garbage collection mechanism".
JavaScript is an excellent scripting language. It contains many features of high-level languages in addition to the flexibility of scripting languages. For example, it is possible to build and instantiate an object, the garbage collection mechanism (GC:Garbage Collecation). Usually we use new to create objects, and GC is responsible for recycling the area of memory occupied by objects. Therefore, understanding GC can deepen the understanding of javascript garbage collection mechanism.
When GC reclaims memory, it first determines whether the object is referenced by other objects. The memory area of the object is freed after it is determined that there are no other object references. Therefore, how to determine that the object is no longer referenced is the key to GC.
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)
As in the above code, A1 is recycled after executing cc (), after which we can pop up the text window through b1.rr. In some basic books, it is explained that A1 is a local variable and b1 is a global variable. Local variables are reclaimed by GC after execution. But this is not always the case. The code is as follows:
Function aa () {this.rr = "pop-up";} function bb () {this.rr = "pop-up";} function cc () {var A1 = new aa (); var b1 = new bb (); return b1;} var b1 = cc (); alert (b1.rr)
At this point, the A1 _ direction b1 in the cc function is a local variable, but the text window still pops up. This indicates that b1 has not been recycled by GC. So local variables in javascript are not recycled by GC all the time.
The GC recycling mechanism needs to be further understood. At this time, several concepts are introduced: two-way linked list, scope chain, active object (to facilitate understanding, simplify the concept of the original text [http://softbbs. Pconline . Com . Cn/9497825 . Html], where the bi-directional linked list describes the upper and lower levels of complex objects. The scope chain and the active object are a node in the two-way linked list, respectively. Take the function cc as an example, the variable hierarchy is:
Windowcca1rrb1rr
When the cc () method is executed, the reference relationship of the variables in memory is shown in the figure above, and the literal explanation is as follows:
The active objects of cc include A1 and b1, and its scope chain is window.
The active objects of cc include A1 and b1, and its scope chain is window.
The active object of A1 includes rr, whose scope chain is cc.
The active object of b1 includes rr, and its scope chain is cc.
When cc () is executed, the execution environment of cc creates an active object and a chain of scopes. Its local variable A1 < b1 will be hung in the active object of cc. When the cc () execution is complete, the execution environment attempts to reclaim the memory occupied by the active object. However, because the local variable b1 passes through return b1, it adds a domain chain: windowb1rr, so GC stops recycling b1. So if you want to promote a local variable / function to global, adding a scope chain to it is OK.
At the same time, it becomes important to control the scope chain of the object. The GC cannot recycle the target object unexpectedly because of the scope chain. For example:
At this point, I believe you have a deeper understanding of "JavaScript garbage collection mechanism", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.