Javascript advanced programming (version 2nd)-Javascript Garbage Collection

Source: Internet
Author: User

JavascriptWe have our own garbage collection mechanism, which saves us the trouble of manual memory collection.

Here we will briefly record the content about this part.

 

JavascriptThere are two garbage collection policies

 

 

I,Mark Clear

This method sets all the variables in the memory to BITs during garbage collection, removes the variables referenced in the execution environment and execution environment, and revokes the remaining variables as useless variables.

 

Note:

Currently, most browsers use the following methods:ProgramSet unused variablesNullThe garbage collector can immediately release the corresponding memory in the next execution.

 

II,Reference count

Is to track the number of variables referenced, when the number of times of reference is0When the variable is no longer used, the memory is reclaimed.

Note:

The memory cannot be released due to the loop reference problem. If the loop size is large, a large amount of memory will be wasted. For example:

Function Test ()

{

VaR O1 = new object ();

VaR O2 = new object ();

O1.obj = O2;

O2.obj = O1;

}

Such as program O1, O2 All 2 , The function will not be reduced 0 So the memory cannot be released.

Solution:

You can manually remove the circular references.

O1.obj = o2.obj = NULL;

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.