In-depth explanation of the JavaScript Memory collection mechanism

Source: Internet
Author: User

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.

 
 
  1. <SCRIPT>
  2. FunctionAA (){
  3. This. RR ="Pop-up window";
  4. }
  5. FunctionBB (){
  6. This. RR ="Pop-up window";
  7. }
  8. VaRB1;
  9. FunctionCC (){
  10. VaRA1 =NewAA ();
  11. B1 =NewBB ();
  12. ReturnB1;
  13. }
  14. CC ();
  15. Alert (b1.rr)
  16. </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:

 
 
  1. <SCRIPT>
  2. FunctionAA (){
  3. This. RR ="Pop-up window";
  4. }
  5. FunctionBB (){
  6. This. RR ="Pop-up window";
  7. }
  8. FunctionCC (){
  9. VaRA1 =NewAA ();
  10. VaRB1 =NewBB ();
  11. ReturnB1;
  12. }
  13. VaRB1 = Cc ();
  14. Alert (b1.rr );
  15. </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:

  
  
  1. Window <=> CC <=> A1 <=> rr
  2. <=> 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:

 
 
  1. <Script language ="JavaScript">
  2. <! --
  3. // Cat
  4. FunctionCAT (name ){
  5. VaRZhuren;
  6. This. Name = Name;
  7. // Set the master
  8. This. Addzhuren =Function(Zr ){
  9. Zhuren = Zr;
  10. }
  11. This. Getzhuren =Function(){
  12. ReturnZhuren;
  13. }
  14. }
  15. // Master
  16. FunctionZhuren (name ){
  17. This. Name = Name;
  18. }
  19. // Create a master:
  20. VaRZR =NewZhuren ("Zhangsan");
  21. // Create a cat
  22. VaRCat1 =NewCAT ("Province");
  23. // Set the host of the cat
  24. Cat1.addzhuren (Zr );
  25. // Release the master
  26. ZR =Null;
  27. // There is still a reference to the master object.
  28. Alert (cat1.getzhuren (). Name)
  29. // -->
  30. </SCRIPT>
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.