Garbage Collection in Javascript
Tags: javascript
Published: 2020-03-01 02:42:09

This is most probably not the first time you're hearing this term. But in this article, I'd be explaining in simple words, exactly what it means, specifically in Javascript.

## So what's garbage collection?

As "garbage collection" literally implies, collecting garbage which refers to irrelevant or unnecessary things.

## What is irrelevant in Javascript?

**MEMORY SPACE!**

Examine the following code:

```js
let name = "Dillion";
name = "Javascript";
```

When `name` was declared, memory space was created to hold the value "Dillion". `name` is just a label to that memory space. Later on, `name` was assigned another value, "Javascript", of which another memory space was created for it.

So what happens to "Dillion"?

It is collected as garbage. This can also be seen as a memory management technique, though not 100% effective. Hence, developers should dearly consider memory management during development and not rely on garbage collection.

## Processes of Memory Usage

There are three processes involved in memory usage.

### 1. Memory Initialization

This refers to assigning space for a value.

```js
var variable1 = 54;
var variable2 = {name: "Javascript"}
```

From the above, memory spaces are assigned for the number type `variable1` and the object type (which is a collection of values) `variable2`.

### 2. Memory Usage

This refers to any form of usage of those values. For example:

```js
var result = variable1 + 23;
```

As seen above, `variable1` which was initialized is now been used.

### 3. Garbage Collection

This is the point where a memory space becomes irrelevant and needs to be collected as garbage.

In lower-level languages, developers would have to explicitly release a memory space but in higher-level languages, this is done automatically in the background with the garbage collection feature.

But the question now is, how does Javascript know when a space is irrelevant?


The general way of doing this is by checking if a value is **REFERENCED** or in another term, **REACHABLE**. The garbage collector engine checks to see if a memory space is referenced and if it isn't, it collects it as garbage.


For example:

```js
var name = "javascript";
name = "James";
var obj = {
    name: "Object"
}
var anotherObj = {
    name: "Another object"
}
var newObj = anotherObj;
anotherObj = 43;
```

1. `name` is declared and a memory space is allocated to the value "javascript". `name` is reassigned another value - "James". All through the program, there is no reference to "javascript" any longer, so it is collected as garbage.

2. `obj` is declared and assigned an object value. The value is always reachable via the `obj` variable, so it is retained.

3. `anotherObj` is declared and assigned an object value. `newObj` is assigned the reference value of `anotherObj` (remember how objects works - mutability). `anotherObj` is assigned a new value - 43. But the object value remains. Why? Because we can still access the object (`newObj.name`).

## Wrap Up

The garbage collection process (which is done automatically and cannot be controlled) is not a 100% method of memory management. Hence, other methods have to be put in place manually.

There are several other algorithms that the garbage collector engine follows to determine if spaces are irrelevant, but the general thing you should note it tries to verify is "Is the memory space reachable or irrelevant?"

Check out these resources for more details:

1. [Garbage Collection | Javascript.info](https://javascript.info/garbage-collection)
2. [Memory Management | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management)