Javascript garbage collection mechanism

Source: Internet
Author: User

People who often use JavaScript will think about its garbage collection mechanism. JavaScript does not require developers to manually clear garbage like C and C ++.ProgramYes. Developers do not need to worry about memory usage issues. The memory allocation and garbage collection are fully realized.Automatic Management. The root cause is that the program collects unused variables and releases the memory occupied by them. Therefore,The garbage collection mechanism periodically and repeatedly performs this operation at a fixed interval.

For example, if a local variable exists only inside the function, the program allocates the corresponding storage space for the local variable in the stack memory or heap memory. When the function is running, the memory occupied by local variables is unnecessary. In this case, the program releases the memory occupied by local variables for other variables. This is the easiest way for a program to release memory, but many times variables in the program will be used all the time. In this case, the garbage collection mechanism must track variables and determine whether they are used, whether the memory space can be released.

The garbage collection mechanism mainly determines whether the variables release the memory space by two methods:One is the mark clearing method, and the other is the reference counting method..

Every variable has its runtime environment. After a variable is created, it runs in a certain environment. For example, when a local variable is created, the local variable runs in the function body. When a function is running, the local variable is marked as "entering the environment". When the function is running, it means that the variable is out of its running environment, in this case, the variable is marked as "out of the environment ".For variables that exit the environment, the garbage collection mechanism records the variables and releases them in the next recycling cycle.

Reference counting method, which records the number of times each value is referenced.When a variable is declared and a reference type value is assigned to the variable, the number of times this value is worth referencing is 1.If the same value is assigned to another variable, the number of times the value is referenced increases by 1. On the contrary, if another value is obtained for a variable that contains a reference to this value, the number of times of reference is reduced by 1..When this value is 0, it indicates that there is no way to access this value, so the occupied memory space can be recycled.. When the spam collector runs in the next cycle, the memory space occupied by the value of zero reference count is released. (For the original article, refer to: javascript advanced programming-version 2)

For example:
Function countmethod (){
VaR object1 = new object (); // declare the variable. The counter changes from 0 to 1.
VaR object2 = new object (); // declare the 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 exits, the counter reading of object1 is 1, and the counter degree of object2 is 1. Therefore, neither of the two variables will be destroyed. If a large number of such programs exist in the function body, a large amount of memory will be wasted and cannot be recycled, leading to memory leakage.

To solve the above problem, manually release the memory occupied by object1 object2. That is:
Object1.method1 = NULL;
Object2.method2 = NULL;

Compare the above example to a normal example.
Function countmethod (){
VaR object1 = new object (); // declare the variable. The counter changes from 0 to 1.
VaR object2 = new object (); // declare the 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 read to 0
}
As shown in the preceding example, normally, when the function stops running, the object1 object2 reads 0. In the next garbage collection cycle, the object1 object2 is recycled and the occupied memory is released.

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.