Javascript has Garbage Collection, which means that the execution environment is responsible for managing the memory used during the execution of the code. During the development process, memory allocation and free memory can be ignored. But triggering the immediate collection mechanism interrupts the execution of the code, stops other operations, traverses all objects, and reclaims all unreachable objects, so garbage collection works periodically.
Because this article is for the quick understanding of memory management, so related knowledge: scope chain, closures and other concepts I will not do here, want to know friends can directly browse the relevant articles in Gold, there are many excellent articles worth reading
The life cycle of a variable
If the garbage collection mechanism determines that the local variable can be collected, then the local variable will disappear from memory.
function fn1(){
var obj = {name: 'richard'}}function fn2(){
var obj = {name: 'jungkkki'}
return obj
}
var a = fn1() // undefined
var b = fn1() // {name: 'jungkkki'}
Copy the code
Fn1 declares and assigns a value to the local variable obj in the function. After the end of the function, the local variable is no longer accessible. When the garbage collection mechanism runs periodically, the local variable obj will be collected.
In declare a local variable in a function fn2 obj and assignment, at the end of the function, the local variable return value assigned to a global variable b, when garbage collection mechanism run periodically, {name: ‘jungkkki} memory will not be recycled.
For interpreted languages (such as JavaScript), you can begin to interpret execution by using lexical analysis -> syntax analysis -> syntax tree.
Syntax analysis into AST (Abstract Syntax Tree), you can try here http://esprima.org/
Mark clear & reference count
How does javascript determine whether or not a variable is accessible
function test(){
var one = {num: '1'} // Tag into the environment
var two = {num: '2'} // Tag into the environment
}
test() // When the function finishes executing one,two, and leaves the environment
Copy the code
Tag clearance: Javascript usually determines whether a variable has been reclaimed by marking its state. When a variable is declared in a function, the tag enters the environment. At the end of the function, the environment is destroyed and the tag leaves the environment waiting for reclamation. As long as the variable that enters the environment is not released, it can be accessed anywhere in the environment at any time and is not collected by the garbage collector.
function test2(){
var a = {name: 'richard'} // {name: 'Richard'
a = {name: 'jungkkki'} // {name: 'Richard '} {name:' Richard '} 'jungkkki'} +1
}
test() {name: 'jungkkku'} wait for reclaim
Copy the code
Reference count :javascript maintains a table that stores the number of references to resources in memory. The resource is referenced +1, the end reference or the end of the function execution scope, the number of references -1, the number of references from 1 to 0 will not be executed, saving overhead, direct markup
Defects in GC, generational collection, and incremental GC
Like any other language, GC interrupts code execution and stops other operations. This operation can take more than 100ms to iterate through all the objects and reclaim all the unreachable objects. Two optimization methods are introduced in the new version of V8 engine: 1. Generation GC, 2. Increment GC
Generational collection: The purpose is to distinguish new generation objects from old generation objects by their usage frequency and duration. More young generation and less Tenured generation are collected to reduce the number of objects that need to be traversed each time, thus reducing the time of each GC
Incremental GC: Runs long traversal and collection operations separately, reducing interrupt time but increasing context switch overhead.
Like friends can easily click like and follow, with everyone’s support will have more power