Some JS advanced study notes--JS memory management

JS memory management

(1)

JavaScript allocates memory for us when variables are defined.

The memory allocation of basic data types by JS will be allocated directly in the stack space during execution ;

JS will open up a space in the heap memory for the allocation of complex data type memory, so that complex data types (arrays/objects) can refer to this space

(2) JS garbage collection (Garbage Collection, GC for short)

For those objects that are no longer used, we call them garbage, and they need to be recycled to release more memory space;

The garbage collector (GC) uses the GC algorithm to determine which objects are no longer used:

①Common GC algorithm – reference counting

When an object has a reference pointing to it, then the reference of the object is +1, and when the reference of an object is 0, the object can be destroyed; a big disadvantage of this algorithm is that it will generate circular references;

②Common GC Algorithm – Mark Clear

This algorithm is to set a root object (root object), and the garbage collector will start from this root periodically to find all objects that are referenced from the root. For those objects that are not referenced, they are considered unavailable objects; this The algorithm can solve the problem of circular reference very well;

Guess you like

Origin blog.csdn.net/weixin_53737663/article/details/127002042