Memory allocation
There are 8 types of JS :Number, String, Boolean, Undefined, Null, Symbol, bigInt, Object, where Number, String, Boolean, Undefined, Null, Symbol, bigInt are Basic type: Object(JSON, Array, Date) is a complex type. If the JS engine encounters a value of the underlying type while parsing code, it will create a space in stack memory (fixed size) to store the value. If a value of a complex type is encountered, a stack memory (of variable size) is used to store the data of the complex type, and a stack memory (of fixed size) is used to store the address of the heap memory space. When using data, if the data type is base, it is read directly from stack memory. If the data is of a complex type, the stack memory address of the data in the heap memory is first used, and then the heap memory address is used to find the data (by reference). Stack memory space by the OS distribution operation, the data stored in accordance with the advanced after the use of l1, when the data call, automatic operation by the OS release space heap memory space allocated by the programmer to use the second level cache, if the programmer doesn’t take the initiative to release, so need to wait for the program at the end of the OS according to recycle after garbage collection algorithm to judge the life cycle.
// overlay b's stack memory over A's stack memory
a = b;
Copy the code
WeakMap and weakSet references in ES6 are weak references and will not be included in garbage collection mechanism. As long as the reference is cleared, the internal reference will be cleared by garbage collection.
The garbage collection
In general, garbage data collection can be divided into manual collection and automatic collection. JS is an automatic collection strategy. Because the overhead of garbage collection mechanism is high, the garbage collector will periodically perform collection at a fixed interval. Local variables exist in the variable object in its function execution context. When the function execution is completed, the function execution context is off the stack, and the garbage collector determines the variables in the variable object and collects them. Global variables exist in the variable object of the global execution context, they exist throughout the lifecycle of the window, and it is difficult for the garbage collector to determine global variables.
Garbage collection algorithms determine whether the memory occupied by variables is used for recycling, and commonly used algorithms include reference counting and mark clearing.
Reference counting
Determines whether an object has a reference to it and retrieves it if not. But there is a problem: circular references. If two objects refer to each other, the garbage collector will not collect them and may end up leaking memory. This is why modern browsers no longer use this algorithm.
Mark clear
Objects in memory are scanned periodically from the root (global objects in JS). Objects that can be reached from the root are retained. Objects that cannot be reached from the root are marked as unused and recycled later.
- Variables are marked “in” when they enter the environment and “out” when they leave the environment
- The garbage collector marks all variables stored in memory at run time (global objects, variable objects, live objects)
- It then removes the tags (closures) of variables in the environment and those referenced by variables in the environment, that is, removing the tags of variable objects in the context of function execution on the execution stack
- Variables marked after this point are considered to be ready for deletion (periodic scan)
- The garbage collector does the cleaning, destroying the tagged values and reclaiming the memory they occupy
The GC defects
When garbage collection is performed, stop responding to other operations (single thread). In this case, generational collection can be used to distinguish between temporary and persistent objects (recycle more temporary objects, recycle less persistent objects, reduce the number of objects traversed each time, thus reducing the time of each GC) to avoid the long stop response caused by GC.
A memory leak
For continuously running server processes, memory that is no longer needed must be released in a timely manner. Otherwise, the memory footprint becomes higher and higher, which may affect system performance at best or cause process crashes at worst. A memory leak is a failure to release memory that is no longer needed.
- Unexpected global variables that the garbage collector will not clean up
- Closures. Variables referenced by closures are kept in memory
- Undestroyed timer and callback functions
- Improper DOM references