directory
- JavaScript memory Management
- Why should memory be managed?
- Memory management Concepts
- Memory management in JavaScript
- JavaScript memory life cycle
- JavaScript garbage collection
- JavaScript garbage collection concept
- JavaScript references and reachability examples
- What are reachable objects?
- What is a garbage collected object?
- Learn about the garbage collection mechanism — GC
- JS executes the GC algorithm commonly used by V8
- Memory management monitoring tool — Performance
JavaScript memory Management
Why should memory be managed?
If we write code without understanding some of the mechanisms of memory management, we can write code with memory problems that are not easily detected. More of this kind of code will bring some unexpected bugs to the program. So it is necessary to master memory management.
Memory management Concepts
- Memory: composed of read and write units, representing a piece of operational space
- Management: to manipulate the application, use and release of a space artificially
- Memory management: Developers actively apply for space, use space, and release space
- Management process: Apply — > use — > Release
Memory management in JavaScript
It’s the same as any other language process: apply, use, release
However, because ECMAScript does not provide the corresponding API, js developers cannot actively call the API to complete space management.
JavaScript memory life cycle
/ / application
let obj = {}
/ / use
obj.name = 'xm'
/ / release
obj = null
Copy the code
JavaScript garbage collection
JavaScript garbage collection concept
- Memory management is automatic. Whenever we create objects, arrays, etc., it automatically matches the memory space.
- Objects are garbage when they are no longer referenced.
- Objects already exist, but these objects cannot be accessed from the root when they are garbage.
At this time, JavaScript will automatically free and recycle the garbage space, that is, JavaScript garbage collection.
To object
- Objects that are accessible are reachable objects, either by reference or by looking up through the scope chain of the context
JavaScript
The root can be understoodGlobal variable objectThe criterion of reachability is whether it can be found from the root
JavaScript references and reachability examples
What are reachable objects?
// The following object space is referenced by obj, and the current object is reachable
let obj = { name: 'xm' }
// In this case, the reference value will change, and ali will also refer to the object space
let ali = obj
// obj terminates the reference to the object space, but ali can still refer to the object space, and that space is still reachable
obj = null
Copy the code
What is a garbage collected object?
// Reachable object
function objGroup (obj1, obj2) {
obj1.next = obj2
obj2.prev = obj1
return {
o1: obj1,
o2: obj2
}
}
let obj = objGroup({name: 'obj1'}, {name: 'obj2'})
console.log(obj)
/ / {
// o1: { name: 'obj1', next: { name: 'obj2', prev: [Circular] } },
// o2: { name: 'obj2', prev: { name: 'obj1', next: [Circular] } }
/ /}
Copy the code
The reachable object for the above example is illustrated below
And then we do something
delete obj.o1
delete obj.o2.prev
Copy the code
Then obj.o1 will become garbage and the JavaScript engine will find the object to collect.
Learn about the garbage collection mechanism — GC
GC is short for garbage collection mechanism, which finds garbage in memory and frees and reclaims space. This algorithm can be seen in all languages, not just JS, the following details about GC algorithms are what?
GC — Garbage collection mechanism understanding and algorithm detailed explanation
JS executes the GC algorithm commonly used by V8
V8 is a major JavaScript execution engine that uses a number of GC algorithms internally, so take a look at its internal garbage collection strategy in detail
Garbage collection mechanism in JavaScript engine V8
Memory management monitoring tool — Performance
Front-end memory monitoring — using the Performance tool