Make writing a habit together! This is the 15th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

When it comes to the js garbage collection mechanism, we may not feel that every time we write code, we are not doing anything. But how does the js garbage collection work?

Memory life cycle

  • Memory allocation: When we declare variables, functions, and objects, the system automatically allocates memory for them
  • Memory usage: reading and writing memory, that is, using variables, functions, etc
  • Memory reclamation: Garbage collection automatically recycles unused memory after use (global variables are generally not recycled, and values of local variables that are no longer used are automatically recycled)

Memory allocation

// let a = 11 let b = 'wya' // let obj = {name: 'wya', age: Function fn(a, b) {return a + b}Copy the code

Garbage collection algorithm

The core idea of garbage collection is to determine whether memory is no longer in use, and if so, treat it as garbage and free it. The main methods used are reference counting and scale counting

In the early days, reference counting was the main method, but now most of them are token elimination methods (IE still uses reference counting method).

Let’s do an example in code

Let obj = {name: 'xiao wang ', age: 23} let p = objCopy the code

Here the complex data in the heap has two referencers, so its reference count is 2, so it is not recycled by JS.

And then let’s give new values to obj and p, so that they don’t keep referring to this complex data

obj = 1
p = null
Copy the code

That’s when they cut off the complicated dataReference, the reference count is zero,This time will be considered useless js data, will be recycled!1 and null have references to obj and p, all of which have a reference count of 1. Js will only reclaim data with a reference count of 0

The reference counting algorithm is a simple and efficient algorithm, but there is a big problem with circular reference, which can be demonstrated in code:

function fn() {
let a = {}
let b = {}
a.name = b
b.name = a
}
fn()
Copy the code

Here when the fn function call is finished, we in the global access function inside the variable is not access, so the function is finished after it declared variables should be recycled, if not destroyed, we each execute this function will open up an object in memory, which will be a great waste of memory!

If we use reference counting here, the changes in the function will not be recycled, so let me draw a picture to show that

If there are only two empty objects declared in the function, we can’t access them globally when the function is finishedThe variable will be destroyed,The reference will disappear and the data will be recycled

When properties in objects A and B refer to each other, even though a and B destroy no reference data, the data is referred to each other, and their * reference count is always greater than or equal to 1. Js will not reclaim the memory data, which is called circular reference.

Hence the tag elimination method described below

  • The tag clearing algorithm defines “not in use object” as “unreachable object”.
  • In simple terms, this means that objects in memory are scanned from the root (or global objects in JS) when they are defined.
  • Anything that can be reached from the root is still needed. Objects that cannot be reached from the root are marked as unused and recycled.

Summary: from the js root (global object), can access to the object, ordinary variables, functions, etc., are needed, will not be released. But if the js root is inaccessible and unreachable (no one can find the space), the space is garbage and needs to be recycled