Javascript garbage collection mechanism Introduction to understanding the basics

Source: Internet
Author: User
Tags garbage collection memory usage

People who use JavaScript often think of their garbage collection mechanism, and JavaScript does not require developers to manually remove garbage as c,c++ does, and when writing JavaScript programs, developers don't care about memory usage. The required memory allocation and garbage collection are fully automated. The main reason is that the program collects the variables that are no longer in use and frees up the memory they occupy. As a result, the garbage collection mechanism performs this operation periodically and repeatedly, at fixed intervals.


For example, local variables exist only within the function, the program will be local variables in the stack memory or heap memory allocation of the corresponding storage space, when the function is finished, local variables occupy the memory is not necessary, then the program will release the memory occupied by local variables for other variables to use. This is the easiest way to release memory, but in many cases, variables are used all the time, and the garbage collection mechanism must keep track of variables and determine whether they are in use and whether they can free up their memory space.

The garbage collection mechanism mainly determines that the variable frees the memory space by two methods: one is the mark elimination method, the other is the reference counting method.

notation, each variable has its own operating environment, the variable is created in an environment, such as creating a local variable, local variables will run in the body of the function. When the function runs, it marks the local variable as "entering the environment", and when the function body is finished, it means that the variable is out of its running environment, then the variable is marked as "away from the environment." For variables that leave the environment, the garbage collection mechanism records them and releases them at the next recycle cycle.

Reference notation, which tracks the number of times each value is referenced. When you declare a variable and assign a reference type value to the variable, the number of references is 1. If the same value is assigned to another variable, the number of references to that value is 1. Conversely, if a variable that contains a reference to this value gets another value, the number of references is reduced by 1. When this worth of reference is 0 o'clock, there is no way to access this value again, so you can reclaim the memory space it occupies. When the garbage collector runs in the next cycle, the memory space occupied by values with a reference number of zero is freed. (Original explanation Reference: Javascript Advanced Programming-second Edition)

For example:

Copy Code code as follows:

function Countmethod () {
var object1 = new Object (); Declaring a variable, the counter is changed from 0 to 1
var object2 = new Object (); Declaring a variable, the counter is changed from 0 to 1
Object1.method1 = Object2; Object1 Counter -1,object2 counter +1
OBJECT2.METHOD2 = Object1; Object1 Counter +1,object2 Counter-1
}

After this function runs out, the Object1 counter reads 1,object2 with a counter of 1. So two variables will not be destroyed. If a large number of such programs exist in the body of a function, it can cause a lot of memory to be wasted and can not be recycled, resulting in memory leakage.

The above problem-solving method manually frees the memory occupied by the Object1 Object2. That
Copy Code code as follows:

OBJECT1.METHOD1 = null;
OBJECT2.METHOD2 = null;

Compare the example above to give an example of a normal situation.
Copy Code code as follows:

function Countmethod () {
var object1 = new Object (); Declaring a variable, the counter is changed from 0 to 1
var object2 = new Object (); Declaring a variable, the counter is changed from 0 to 1
OBJECT1.METHOD1 = "This is Object1"; Object1 counter -1,object1 reading becomes 0
OBJECT2.METHOD2 = "This is Object2"; Object2 counter -1,object2 reading becomes 0
}

The above example shows that normally, when the function is finished, the Object1 object2 reads 0, is reclaimed and frees up the memory it occupies during the next garbage collection cycle.

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.