Garbage Management in JS

Garbage Management in JS

Before moving ahead, in JavaScript, memory management is more relevant terminology than garbage management.

Garbage collection is a part of memory management and is handled automatically by the JavaScript runtime environment.

In JavaScript, every function, Object , variables we create, all that takes memory.

And if you have worked with heap, or deleting a linkedlist node, you might came across freeing/releasing of memory,
But ever wondered how does JavaScript clean it up?

Reachability

It is the basic concept of Garbage Collection.

In the context of garbage collection, reachability refers to the idea that an object is considered reachable if there is a reference or chain of references from a root object (e.g., a global variable, a local variable, or a static variable) to that object.

Garbage collectors use reachability analysis to determine which objects are still in use and which can be safely reclaimed.

This process helps in efficient memory management by reclaiming memory occupied by objects that are no longer needed, preventing memory leaks and optimizing overall system performance.

Let's understand:

let owner = {
  "name": "Aryan"
  "age": 18
};

In this, owner has the reference to the object and value.

A simple example

If you are done with LinkedList, then you might know how each node has address of the node next to it.
And that is the reference to next node.

temp -> next = head;

And if the reference is Null, it breaks and the preeceding node will lose its reference.

head -> next = NULL;

More than One references

Imagine for a moment that we moved the reference from the name to the Information:

let name = {
  name: "Aryan"
};

let Information = name;

What's benefit?🤔

user = null;

Here it is, imagine we give null to name, even then we could access it using Information.

Approach 2:

temp -> next = head;
curr -> next = head;
//this is cpp code, pardonme if -> syntax is wrong

Here, we could access head using both curr and temp pointer from the nodes.

Linkage of Objects

It is more advanced form for linking of objects.

Let's understand it with the help of an example:

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

  return {
    father: man,
    mother: woman
  }
}

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

Function marry “marries” two objects by giving them references to each other and returns a new object that contains them both.

As of now, all objects are reachable.

Now let’s remove two references:

delete family.father;
delete family.mother.husband;

It’s not enough to delete only one of these two references, because all objects would still be reachable.

But if we delete both, then we can see that John has no incoming reference any more:

Outgoing references do not matter. Only incoming ones can make an object reachable. So, John is now unreachable and will be removed from the memory with all its data that also became unaccessible.

Unreachable nodes

It is possible that any node that remains unreached or no linking from other nodes to that particular node.

We find it while deleating node in a LinkedList.

After deleating we free the memory it holds.

Summary

Garbage collection in programming languages is an automatic process that manages memory by reclaiming unused objects.

Objects remain in memory as long as they are reachable, but being referenced does not guarantee reachability.

Modern engines use advanced garbage collection algorithms. This information is crucial for low-level optimizations, making it beneficial to explore after gaining familiarity with the language.

Let's end this blog....

From now on, code with style

See u in the next blog...Stay tuned🎶

Don't forget to follow me on:
Twitter And LinkedIn