memory
Memory is used to temporarily store computing data in the CPU and data exchanged with external storage such as hard disks. It is the bridge between external memory and CPU
V8
-
V8 is a mainstream JavaScript execution engine
-
With just-in-time compilation (converting to machine code), other engines are executed before being converted to machines
-
V8 garbage collection strategy
- Generational recycling
- Space to copy
- Mark clear
- Tag to sort out
- Mark the incremental
Memory management
-
Memory: Composed of read and write units, representing a piece of operable space, we usually create basic types, objects, arrays, etc., need memory. Since there is use, there will be management. Memory management in JS is divided into three steps: 1. Applying for memory space; 2. Free up memory space.
In JS, you can’t actively call apis like C and C ++ to manage
Let obj = {} assignment automatically allocates a block of memory
Obj. name=”lg” Space used
obj = null; Free memory
To object
-
Accessible objects are reachable objects (references, scope chains)
-
The criterion of reachability is whether it can be found from the root
-
Js global variable object is the root (global context)
The garbage collectionJane’s book
-
Js memory management is automatic
-
An object is garbage when it is no longer referenced (when some object cannot be found by reference)
-
When an object cannot be accessed correctly (object exists, object cannot be found due to syntax or structural errors)
-
GC algorithm
-
Find garbage in memory and free and reclaim space
- At run, when the context is no longer needed
- In code, the external environment cannot find the object
-
Common GC algorithms
-
Reference counting
-
Each object is added a reference counter that identifies the number of times the object is referenced, incremented when a new reference points to the object, decayed when a reference to the object is invalid, and immediately reclaimed when it reaches zero
-
Advantages:
- immediately
- Minimize program pauses
-
Disadvantages:
- Object referenced by loop cannot be recycled
function fn() { const obj1 = {} const obj2 = {} Obj1 and obj2 are not found in the global scope, but obj1.name and obj2.name are still referenced, so the number of references is not 0 obj1.name = obj2; obj2.name = obj1 return 'xxxx' } fn() Copy the code
- High time overhead (maintenance value changes (listening), object changes, reference counts also need to change)
-
-
Mark clear
-
It is completed in two stages: marking and clearing
-
Walk through all the objects looking for the mark active object
- The tag count recursively looks up the reference hierarchy and marks the reachable objects
-
Iterate over all objects to remove unmarked objects
-
Clears unmarked objects
-
The flag cleared puts reclaimed space on the free list, making it easier for later programs to apply for memory directly there.
-
-
Reclaim space
-
advantages
- Fixed object circular references not being recycled (as opposed to reference counting)
-
disadvantages
- Space fragmentation (due to discontinuous space addresses recovered) cannot be space maximization
- Garbage is not immediately removed
-
-
Tag to sort out
- The tag cleaning algorithm is an upgrade of the tag cleaning algorithm. The main purpose is to reduce defragmentation. It works in the same way as the tag clearing algorithm.
- In contrast to tag cleanup, garbage objects are moved before untagged objects are cleaned, which reduces the fragmentation space
-
Generational recycling
-
Memory limit: The 64-bit memory size is less than 1.5 GB, and the 32-bit memory size is less than 800 MB
-
Memory space is divided into Cenozoic (short survival time) and old generation
-
Little space to store the new generation object (32 m [64] [32] | 16 m)
-
New generation object recovery
-
The recycling process adopts replication algorithm + label finishing
-
The new generation of memory is divided into two equal size Spaces
-
Use the space bit From and the free space bit To
-
Live objects are stored in the From space
-
Copy the live object To To after the tag is collated
-
The space exchanged between From and To is released
Recycling details
- Promotions may occur during copying
- Promotion is the movement of the new generation to the old generation
- A new generation of GC still alive needs to be promoted
- To space usage exceeds 25%
-
-
S old object (1.4 G [64] | 700 m [32])
- An old age object is an object that has a long life
- The main use of clear mark, mark finishing, incremental mark algorithm
- Garbage space is first (primarily) collected using tag cleanup
- Spatial optimization was carried out by label finishing
- Incremental markers are used to optimize efficiency
-
-
-
-