The garbage collection mechanism in JavaScript

Source: Internet
Author: User
Tags garbage collection

What is JS garbage collection? (what)

JavaScript also has an automatic garbage collection mechanism (gc:garbage Collection);

Because memory content is extremely limited, the garbage collector periodically (periodically) finds variables that are not in use and frees up memory

Variables that are no longer used are generally only local variables, that is, when the function execution ends, the memory used by the local variable is recycled, and the inner function in the closure, of course, occupies the local variables of the external function.

What are the garbage collection methods? (how)

Which of the variables is useless? So the garbage collector has to keep track of which variables are useless, mark the variables that are no longer useful, and get their memory back in the future.

Policies that are used to mark useless variables may differ by implementation, typically in two ways: Mark Purge and reference count. Reference counts are less common, and tag cleanup is more commonly used.

1. Mark Clear:

The garbage collector will tag all variables stored in memory at run time (of course, you can use any markup method). It then removes variables from the environment and tags (closures) of variables referenced by variables in the environment. Variables that are tagged later will be considered as variables to be deleted because variables in the environment are inaccessible to those variables. Finally, the garbage collector completes the memory cleanup work, destroys those tagged values, and reclaims the memory space that they occupy.

So far, the JS implementations of IE, Firefox, Opera, Chrome, and Safari are all using a garbage collection strategy or similar strategy for tag cleanup, except that the time interval for garbage collection is different.

2. Reference count:

The meaning of the reference count is the number of times each value is referenced by the tracking record. When a variable is declared and a reference type value is assigned to the variable, the number of references to that value is 1. If the same value is assigned to another variable, the number of references to that value is added by 1. Conversely, if a variable that contains a reference to this value has another value, the number of references to the value is reduced by 1. When the number of references to this value becomes 0 o'clock, it means that there is no way to access the value again, so that it can reclaim the memory space it occupies. In this way, the next time the garbage collector runs, it frees the memory that is used by those values that have a reference count of 0.

When does garbage collection take place? (when)

The garbage collector runs periodically, and if you allocate a lot of memory, the recycling process can be daunting, and determining the garbage collection interval becomes a problem worth thinking about. IE6 garbage collection is run according to memory allocation, when there are 256 variables in the environment, 4,096 objects, 64k strings in any case, it will trigger the garbage collector to work, looks very scientific, do not have to call once a period of time, sometimes it is not necessary, so on-demand call is not very good? But if there are so many variables in the environment, and now the script is so complex and normal, the result is that the garbage collector has been working, so the browser is not going to play.

Microsoft IE7 in the adjustment, the trigger condition is no longer fixed, but dynamic modification, the initial value and IE6 the same, if the garbage collector to reclaim memory allocation is less than 15% of the memory of the program, the majority of memory can not be recycled, set garbage collection trigger conditions are too sensitive, when the street conditions doubled, If the recovered memory is higher than 85%, it means that most of the memory should be cleaned up, and then the trigger condition is set back. This makes the garbage collection work a lot of functions.

The above three points are about the knowledge of the garbage collection mechanism in JavaScript. Feel good to collect ~ ~

The garbage collection mechanism in JavaScript

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.