JavaScript Garbage collection
Tags: javascript, codenewbie, webdev, tutorial
Published: 2021-03-13 00:36:57

A garbage collector monitors all objects and removes those that have become unreachable.

Memory management is crucial in programming that is why the JavaScript engine removes or cleans up all objects that aren't reachable.

We've learned about garbage collection in the previous articles but let's go a little further below:

---
### Reachability
Programs ran on the script are stored in memory for accessibility or *reachability*. 

Let's see a couple of examples below:

```js
const person = {
  name: 'Bello'
};
```
In the example above `person` references the object `{ name: 'Bello' }` but what if it is changed or overwritten. 

See the example below:

```js
let person = {
  name: 'Bello'
};

person = null;
```

In the example above, `Bello` is now unreachable because there are no references to it. This is where the **Garbage collector** discards the data to free the memory.

#### Two references
It is possible to set an object to `null` and still reference it provided it is already copied to another variable.

See the example below:

```js
const person = {
  name: 'Bello'
};

let user = person;
user = null;

console.log(person.name); // Bello
```

In the example above, the object `person` is still reachable but through the copied object, `user`.

#### Interlinked Objects

See the example below:

```js
const marry = (man, woman) => {
  woman.husband = man;
  man.wife = woman;

  return {
    father: man,
    mother: woman
  }
}

const family = marry(
  { name: "Bello" },
  { name: "Monica" }
);

console.log(family);

/*
{
  father: { name: 'Bello', wife: { name: 'Monica', husband: [Circular] } },
  mother: { name: 'Monica', husband: { name: 'Bello', wife: [Circular] } }
}
*/

delete family.father; // true
delete family.mother.husband; // true

console.log(family) // { mother: { name: 'Monica' } }
```

The `[Circular]` means there's an interlinked (closed-loop) relationship between objects.

> Circular reference is a series of references where an object references itself directly or indirectly through a series of objects, resulting in a closed-loop, appearing in most computer programming including JavaScript - [Akinjide Bankole](https://www.akinjide.me/2018/circular-reference-in-javascript/#:~:text=Circular%20reference%20is%20a%20series,most%20computer%20programming%20including%20JavaScript.&text=stringify%2C%20transforms%20a%20JavaScript%20value,string%20by%20traversing%20values%20within.)

All objects above are reachable even after the following code occurred

```js
delete family.father;
delete family.mother.husband;
```

But if we delete the father object from the family object then `Bello` has no incoming reference anymore.

```js
delete family.father;

console.log(family)

/*
{
  mother: { name: 'Monica', husband: { name: 'Bello', wife: [Circular] } }
}
*/

console.log(family.father) // undefined
```

Since Bello is unreachable it gets removed from memory by the garbage collector. Its data also became inaccessible.


#### Unreachable island
The interlinked objects become unreachable if the global object (`family`) is initialized to `null`

See the example below:

```js
const marry = (man, woman) => {
  woman.husband = man;
  man.wife = woman;

  return {
    father: man,
    mother: woman
  }
}

let family = marry({
  name: "Bello"
}, {
  name: "Monica" 
});

family = null;

console.log(family) // null
```

Setting the global object, `familly` to `null` makes the whole island of interlinked objects above removed from memory.

---
[![Buy me a Coffee](https://res.cloudinary.com/bizstak/image/upload/v1610317510/ko-fi-trans_fr3su4.png)](https://www.buymeacoffee.com/techstackmedia)

---