The garbage collection mechanism of JavaScript

Source: Internet
Author: User

In JS, the allocation of required memory and the recovery of useless memory are fully realized automatically . Garbage collection mechanism principle: to identify those variables that are no longer used, and then release the memory they occupy, the garbage collector performs this operation periodically at a fixed time interval (or a scheduled collection time in code execution) there are two ways to do this garbage collection: Tag cleanup and reference counting.

  tag Cleanup : Add tags to all variables stored in memory, remove variables from the environment, and tags referenced by variables in the environment, and the remaining tagged is the variable you are going to delete, which is a more common method that most browsers use.

  Reference Counting Method : Declares a variable, assigns a value of a reference type to the variable, the variable has a reference number of 1, if the variable is assigned to another variable, then the variable reference number plus 1, if the variable containing the reference to the variable gets a new value, then the number of references to the variable is reduced by 1 ; If the number of references is 0, it is considered garbage collection. This method is less common,IE9 the following DOM and BOM is the garbage collection method , the method when encountering a circular reference will produce a memory leak, resulting in the destroyed variable memory space can not be recycled, for example:

 1  function   Test () { 2   var  a=new  Object; // a The number of references is 1  3  var  b=new   Object; // b The number of references is 1         4  a.r=b; // b The number of references is 2         5  b.r=a; // a The number of references is 2  6 } 

A and B refer to each other, and the last two variables have a reference count of 2, so when the function finishes, even though a variable is destroyed, the freed memory cannot be reclaimed because they have a reference count of 2. So in IE9 the following to do the DOM and BOM need to pay attention to this problem, and the other objects when the IE is also used to clear the tag, do not worry about circular reference problems,IE9 above has been the DOM and BOM objects completely converted to JS object , do not worry about this problem.

When there are global variables and global objects that are no longer used, it is best to manually set them to null by dereferencing the recycle mechanism.

  

The garbage collection mechanism of JavaScript

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.