JavaScript — memory management and garbage collection

Time:2021-10-22

catalogue

  • JavaScript memory management

    • Why does memory need to be managed?
    • Memory management concepts
    • Memory management in JavaScript
    • JavaScript memory life cycle
  • JavaScript garbage collection

    • JavaScript garbage collection concept
    • Reference and accessibility examples of JavaScript

      • What are reachable objects?
      • What are garbage collection objects?
  • Understand garbage collection mechanism – GC
  • GC algorithm commonly used by JS execution engine V8
  • Memory management monitoring tool – Performance

JavaScript memory management

Why does memory need to be managed?

If we don’t understand some mechanisms of memory management when writing code, we will write some code with imperceptible memory problems. This kind of code will bring some unexpected problems to the programbug。 Therefore, it is necessary to master memory management.

Memory management concepts

  • Memory: it is composed of read-write units and represents an operable space
  • Management: artificially operate the application, use and release of a space
  • Memory management: developers actively apply for space, use space and free space
  • Management process:Application – > use – > release

Memory management in JavaScript

Like other language processes:Application, use and release

But becauseECMAScriptNo correspondence provided inAPI, sojsIt cannot be called by the developer activelyAPIComplete space management.

JavaScript memory life cycle

//Apply for
let obj = {}
//Use
obj.name = 'xm'
//Release
obj = null

JavaScript garbage collection

JavaScript garbage collection concept

  • Memory management is automatic. Whenever we create objects, arrays, etc., it will automatically match the memory space.
  • Object is no longerquoteIt’s rubbish.
  • Objects already exist, but they cannot be retrieved fromRoot accessIt’s rubbish.

This timeJavaScriptIt will automatically release and recycle the space of these garbage, i.eJavaScriptGarbage collection.

Reachable object

  • The accessible objects are reachable objects, which can be found by reference or through the scope chain of the context
  • JavaScriptThe root of can be understood asGlobal variable object, the attainable standard is whether it can be found from the root

Reference and accessibility examples of JavaScript

What are reachable objects?

//The following object space is referenced by obj, and the current object is reachable
let obj = { name: 'xm' }

//The reference value here will change. At this time, Ali also refers to the object space
let ali = obj

//Obj's reference to the object space is terminated, but Ali can still reference the object space, which is still reachable
obj = null

What are garbage collection objects?

//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] } }
//}

The reachable object of the above example is shown below

JavaScript -- memory management and garbage collection

Then do some operations

delete obj.o1
delete obj.o2.prev

thatobj.o1It will become garbage,JavaScriptThe engine will find this object for recycling.

JavaScript -- memory management and garbage collection

Understand garbage collection mechanism – GC

GCShort for garbage collection mechanism, it can find garbage in memory and free and recycle space. This algorithm can be seen in all languages, not justJS, let’s talk about it in detailGCWhat are the algorithms?

GC — understanding of garbage collection mechanism and detailed explanation of algorithm

GC algorithm commonly used by JS execution engine V8

V8Is a mainstreamJavaScriptExecution engine, which internally uses someGCAlgorithm, you can take a detailed look at its internal garbage collection strategy

Garbage collection mechanism in JavaScript engine V8

Memory management monitoring tool – Performance

Front end memory monitoring – using performance tools