When we create objects, functions and other things, we need a memory to store them, that is, JS memory management is actually automatic, but if we do not need these things, how does JS know? How do you clean them up?

The main concept of memory management in JS is reachability. Reachability refers to ensuring that values that can be accessed or used are kept in memory. Such as some proper values (roots) and values that can be accessed from the roots through references and chains of references. Then these reachable values cannot be deleted, and the remaining values are monitored and deleted by the garbage collector in the JS engine.

Here’s an example:

let user = {
  name: "John"
};
Copy the code

The global variable user now refers to the object {name: “John”}. The properties of the object store a primitive, so it is drawn inside the object. But if the user is overwritten user = null; , the reference is lost, that is, John becomes unreachable, no object can access him, and no reference is made to him, so the garbage collector deletes the data.

Now we will copy the reference from user to admin:

let user = {
  name: "John"
};

let admin = user;
Copy the code

When we execute user = null again; The object is null, but we can still access it through the admin global variable, so it is still in memory. However, if we overwrite admin as well, it will be deleted.

Here’s another complicated example:

function marry(man, woman) {
  woman.husband = man;
  man.wife = woman;

  return {
    father: man,
    mother: woman
  }
}

let family = marry({
  name: "John"
}, {
  name: "Ann"
});
Copy the code

Family. Father = family. Mother = husband = family. Father = family. The answer is still the same, but what if we delete them all? At this time, John only has outgoing references, no incoming references. To put it more generally, you know your idol, but your idol does not know you. At this time, you can only be mercilessly deleted.

Same code, we call family = null; What’s going to happen? At this point, the concept of conceptual accessibility is introduced. Although there are outgoing and incoming references between family members, the family object has been disconnected from the root, so John and Ann and the cross-reference links between them will be deleted.

How does garbage collection work?

The basic garbage collection algorithm is called “tag sweep.”

It periodically performs the following steps:

  1. The garbage collector takes their roots and “marks” them.
  2. Access and “tag” all references from them.
  3. Access the tag objects and the references to tag them. Remember all the objects you’ve accessed so you don’t have to access the same object again in the future.
  4. And so on until all reachable references are accessed (starting at the root).
  5. Deletes all objects except those marked.

This is the concept of how garbage collection works.

Pay attention to
  • Garbage collection is automated. We can’t force it or stop it.
  • When objects are accessible, they remain in memory.
  • Referenced is not the same as accessible (from the root) : a bunch of linked objects may not be accessible.

The source of the article is here

Thanks for watching