js closure and memory leak - collection mechanism

js closure, advantages and disadvantages.
For variable scope of flexible use; global variables and local variables.
js features: internal functions global variables can be read directly, but can not read the local variables inside a function outside the function.
In order to complete closure, local variables inside a function to read from the outside.

F1 function () {
var = n-999;
function F2 () {
the console.log (n-);
}
}
wants to obtain a flexible approach local variables inside a function is, a function is defined inside the function again.
External functions can not read the contents of the internal functions, but internal functions can access global variables, so to get to the contents of the internal function is to define the function becomes a function of the internal functions defined functions. However, the internal variable function with respect to the parent function is not visible. -js language-specific scope chain structure. (Sub-objects will be a level up to find all the variables of a parent object so that all objects parent object for the child objects are visible, but the child object to the parent object is not visible.)

The concept closure: f2 is a function Closure. Action closure is capable of reading the function of internal variables other functions.
js only inside a function Functions can read local variables. Closure is a subroutine internal function. Closures are connected to the bridge function of the internal and external functions.
Action closures: 1 reads the internal variable function. 2. Let the value of the variable remains in memory of them, no longer f1 after the call is automatically cleared. f1 and f2 is the parent function, and f2 is assigned to a global variable, leading to f2 is always present in memory, it depends on the existence f2 f1 f1 always so in memory, does not call after the end of the garbage collection mechanism recovery, garbage collection recovery.

nAdd = function () {n + = 1} First Nadd var not previously used keywords, Nadd is a global variable, rather than the local variable. Second, the value is an anonymous function Nadd, anonymous function itself and a closure, therefore, is equivalent to a Nadd the setter, may operate on local variables inside a function outside the function.

Using closures Note: 1. Due to the closure will make the function of the variables are stored in memory, memory consumption is large, and therefore can not be abused closure, otherwise it will cause performance problems page in ie may cause a memory leak problem . The solution is, before exiting the function will not be used to delete all local variables. 2. Closures function outside the parent, the parent function changes the value of the internal variable. So, if the parent function as an object, the closure as a public method, the internal variable as his private property, then we must be careful, do not change the value of the parent function of internal variables.


js garbage collection:
js in memory, automatic garbage collection mechanism used js execution environment will be responsible for managing the execution of the code.
Life Cycle: When the end of the life cycle of a variable, it points to the memory should be released. js two variables, global variables and functions defined in the generated local variables. The life cycle of a local variable after function execution is over, this time it will release its reference to memory. But global variables will continue to shut down the browser page.

js form of garbage collection: js execution environment garbage collector How can I detect the piece of memory can be recovered, mainly in two ways, mark clear, the reference count.
Clear tag: the tag into the environment when the function is executed, when the function been executed, marking leave the environment. After leaving the environment as well as variable marker that is uncertain way, it may be a particular bit of a reversal or maintenance list.
The garbage collector for all the variables memory are marked, then mark remove environment variables and variables referenced in the environment variable. After that, again combined with the marked variable is the variable need to be recovered, because the environment variables have been unable to access these variables.

Reference counting: in this way can cause a memory leak, use this method in earlier versions of ie. It is worth quoting a mechanism to track the number of times, when you declare a variable and a reference value of the number of citations plus one-time type assigned to the variable when this variable points to a different time, which is worth quoting the number minus one. When the value is 0 when the reference number, will be recovered.

The cause of the leak is not solve the problem of circular references. (Target loop assignment, without the use of an intermediate value)
function S () {var A = {}; var B = {}; a.prop = B; A = b.prop;}

In this case, the result in each call function, a and b are the reference count of 2, this memory will never be released, which causes a memory leak (memory and can not be released or used has been occupied.)

Guess you like

Origin www.cnblogs.com/lxj666/p/11358434.html