JS heap and stack

Stack: automatically allocated and released by the operating system to store function parameter values, local variable values, etc. Basic data types are placed on the stack .
Heap: Generally allocated and released by the programmer. If the programmer does not release it, it will be reclaimed by the garbage collection mechanism . Complex data types are placed in the heap.


Basic data types: number: numeric value, string: string, boolean: Boolean (Boolean value), undefine: Undefined type, null: Null type, symbol: Symbol (symbol)

Complex data types : object, array, function, new

js garbage collection mechanism:

It is an automatic memory management mechanism that can automatically identify variables and objects that are no longer used and clear them from memory to free up memory space.

JS memory life cycle:

1. Memory allocation: When declaring variables, functions, and objects, the system automatically allocates memory for them;

2. Memory usage: reading and writing memory, that is, using variables, functions, etc.;

3. Memory recycling: After use, the garbage collector automatically recycles the memory that is no longer used.

Note : Global variables are generally not recycled (turn off page recycling)

           Under normal circumstances, the value of local variables will be automatically recycled if not used.

Memory leak : If the memory allocated in the program is not released or is not released due to other reasons, it is called a memory leak.

Recycling method : mark clearing, reference counting .

Guess you like

Origin blog.csdn.net/weixin_62635213/article/details/132381187