Original: Learn JavaScript from the beginning (10)--garbage collection
First, garbage collection
1.1javascript garbage collection mechanism:
Automatic garbage collection, the execution environment is responsible for managing the memory used during code execution. In languages such as C and C + +, a basic task for developers is to manually track memory usage, which is a source of many problems. When writing JavaScript programs, developers no longer have to worry about memory usage, the allocation of required memory, and the use of useless recycling to fully automate management.
1.2 Garbage Collection principle:
- Identify the variables that are no longer in use, and then release the memory that is occupied by them.
- The garbage collector periodically performs this operation at a fixed time interval (or as a preset collection time in code execution).
1.3 Garbage Collection methods:
1.3.1 Tag cleanup: When a variable enters the environment, it marks "enter the environment" and when the variable leaves the environment, it marks it as "out of the environment."
1.3.2 Reference count: The meaning of the reference count is the number of times each value is referenced by the trace record. When a variable is declared and the value of a reference type 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 gets another value, the number of references to that value is reduced by 1. When the number of references to this value becomes 0 o'clock, there is no way to access the value, so you can reclaim the memory space it occupies. When the garbage collector runs again, it frees the memory that is used by those values that have a zero reference count.
For example:
1 functionCountmethod () {2 varObject1 =NewObject ();//declares a variable, and the counter changes from 0 to 13 varObject2 =NewObject ();//declares a variable, and the counter changes from 0 to 14OBJECT1.METHOD1 = "This is Object1";//object1 counter -1,object1 reading changed to 05OBJECT2.METHOD2 = "This is Object2";//object2 counter -1,object2 reading changed to 06}
When the function is finished, the Object1 object2 readings are 0, and in the next garbage collection cycle, it is reclaimed and the memory consumed is freed.
The reference count can be imagined as a monkey to break corn, the use of maize is a variable (the reason is the use of rights rather than the corn itself, because the reference count is the value of the reference type), the monkey is the counter. The first monkey got the corn right, counter +1; the second monkey to share the right to use the corn with the first monkey, counter +2; one of the monkeys found a greater use of corn, gave up the right to use the corn, counter-1, another monkey also found a greater use of corn, Give up the right to use the corn, the counter becomes 0, the corn is recycled ~(the example is not appropriate to look at forgive, just to help understand ~)
Let's look at one more example:
1 functionCountmethod () {2 varObject1 =NewObject ();//declares a variable, and the counter changes from 0 to 13 varObject2 =NewObject ();//declares a variable, and the counter changes from 0 to 14Object1.method1 = Object2;//Object1 Counter -1,object2 counter +15OBJECT2.METHOD2 = Object1;//Object1 Counter +1,object2 counter-16}
This example is equivalent to two monkeys have a maize right to use, but always feel that the other hand of the corn in the right to use the corresponding corn than their own, so constantly in exchange for the use of corn. That is, a circular reference. (the example is not appropriate to look at forgive, just to help understand ~)
After this function exits, the counter reading for Object1 is 1,object2 with a counter of 1. So none of the two variables are destroyed. If a large number of such programs exist in the function body, it can result in a large amount of memory being wasted and cannot be recycled, resulting in memory leaks.
The above problem can be eliminated by manually releasing the memory occupied by Object1 Object2. That
1 NULL ; 2 null;
Second, performance issues
Garbage collectors are run periodically, and if the amount of memory allocated for a variable is objective, the amount of recycling is significant. In this case, determining the time interval for garbage collection is a very important issue. When it comes to how often a garbage collector runs, it's reminiscent of IE's notorious performance issues. IE's garbage collector runs according to the amount of memory allocated, specifically 256 variables, 4,096 objects (or array) literals and arrays of elements (slots), or 64KB strings. If any of these thresholds are reached, the garbage collector runs. The problem with this implementation is that if a script contains so many variables, it is likely that the script will remain so many variables throughout its life cycle. As a result, the garbage collector may have to run frequently. As a result, the initial IE7 of the severity performance problem that is raised overrides its garbage collection routines.
The above paragraph can be understood as follows: garbage collector can be regarded as a diligent sanitation workers, memory allocation can be seen as the volume of garbage bins, if a community of residents to build the capacity of garbage is constant, that is, each recycling cycle can be piled up garbage bins, then the sanitation workers will be tired ... (the example is not appropriate to look at the excuse, just to help understand ~)
With the release of IE7, the garbage collection routines of its JavaScript engine have changed the way it works: variable allocations that trigger garbage collection, literals, and (or) critical values of array elements are adjusted for dynamic remediation. The critical values in the IE7 are equal to IE6 when initialized. If a routine reclaims less than 15% of the memory allocated, the thresholds for variables, literals, and/or array elements are doubled. If the routine reclaims 85% of the memory allocations, the various critical resets will be defaulted. This seemingly simple adjustment greatly improves the performance of IE when running pages that contain a lot of JavaScript.
In fact, the garbage collection process can be triggered in some browsers, but it is not recommended. In IE, call window. The CollectGarbage () method immediately points to garbage collection, and in Opera7 and later, calling Widnow.opera.collect () also initiates the garbage collection routine.
Third, management memory
Make sure that you use a minimum of memory to get better performance on your page, preferably by setting its value to null to release its reference-this is called dereferencing (dereferencing). This practice is used for most global variables and properties of global objects. Local variables are automatically dereferenced when they execute the environment, as shown in the following example:
1 functionCreateperson (name) {2 varLocalperson =NewObject ();3Localperson.name =name;4 returnLocalperson;5 };6 varGllbalperson = Createperson ("Nicholas");7 8 //manually de-Globalperson references9Globalperson =NULL;
The last line of code is de-referencing.
Releasing a reference to a value does not mean that the memory consumed by the value is automatically reclaimed. The real effect of dereferencing is to leave the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs.
Just as you take out the rubbish from the home and throw it into the litter bin of the community, the rubbish is just out of your home this execution environment, but is not reclaimed by the garbage dump, only wait for the next cleaning cycle, the industrious sanitation worker uses the garbage truck to pull away to the rubbish dump, only then is to be recycled. (The example is not appropriate to look at forgive, just to help understand ~)
Learn JavaScript from the beginning (10)--garbage collection