Skip to main content

Garbage Collection

Objects the Basics: Garbage Collection


How does JavaScript manage memory manually or automatically?

View Answer:
Interview Response: JavaScript automatically allocates memory when objects get created and clears the memory when the object no longer references it. (Garbage Collection)

note

This automaticity is a potential source of confusion: it can give developers the false impression that they do not need to worry about memory management.


Explain the three steps of the memory life cycle in JavaScript?

View Answer:
Interview Response: The memory life cycle includes allocating, using, and releasing the allocated memory when it is no longer needed.

Technical Response: The three steps in the memory life cycle include allocating memory, using the allocated memory, and releasing the allocated memory when it is no longer needed. The last part is more implicit in JavaScript than in low-level languages.


Code Example:

var n = 123; // allocates memory for a number
var s = 'azerty'; // allocates memory for a string

var o = {
a: 1,
b: null,
}; // allocates memory for an object and contained values

// allocates memory for the array and contained values
var a = [1, null, 'abra'];

function f(a) {
return a + 2;
} // allocates a function (which is a callable object)

// function expressions also allocate an object
someElement.addEventListener(
'click',
function () {
someElement.style.backgroundColor = 'blue';
},
false
);

var n = null;

What is the central concept of memory management in JavaScript?

View Answer:
Interview Response: The central concept of memory management in JavaScript is reachability. Simply put, "reachable" means values that are accessible or usable somehow and are guaranteed to get stored in memory.

Concerning JavaScript memory management. What is a GC root?

View Answer:
Interview Response: A "root" is simply an object that the garbage collector assumes is reachable by default, which then has its references traced to find all other current objects that are reachable. Any object that is not reachable through any reference chain of any root objects is considered unreachable and eventually gets destroyed by the garbage collector.

A base set of inherently reachable values cannot get deleted for obvious reasons. Can you name at least one?

View Answer:
Interview Response: JavaScript cannot delete Global variables directly, and the global variable must be set to null before the memory can be collected. However, the variable still exists and references null (nothingness).

Code Example:

Before:

// global -> {nothingness}

After:

// global -> var a -> object { foo: "bar" }

null:

// global -> var a -> null

Can interlinked objects be garbage collected based on the nullification of a specific object on the GC root in JavaScript?

View Answer:
Interview Response: Yes, the deleted or nullified object gets garbage collected even if it is part of a GC root or one of its properties references another object.

Code Example:

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

return {
father: man,
mother: woman,
};
}

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

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

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

Is it possible that all the interlinked objects in a GC root become unreachable and removed from memory?

View Answer:
Interview Response: Yes, it is possible if the root gets nullified in the program.

Code Example:

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

/**
* below the root has been nullified and all corresponding
* interlinked objects will be garbage collected
*/

family = null;

What is the basic garbage-collection algorithm called in JavaScript?

View Answer:
Interview Response: The basic garbage collection algorithm is called mark-and-sweep.

Explain how the internal JavaScript garbage collector algorithm works?

View Answer:
Interview Response:

The following "garbage collection" steps regularly get performed:

  1. The garbage collector takes roots and "marks" (remembers) them.
  2. Then it visits and "marks" all references from them.
  3. Then it visits marked objects and marks their references. The garbage collector remembers all visited objects so as not to visit the same object twice in the future.
  4. And so on until every reachable (from the roots) reference is visited.
  5. All objects except marked ones get removed.

Can you name the three standard garbage collection optimizations?

View Answer:
Interview Response: The three standard garbage collection optimizations include generalization, incremental, and idle-time collection.

note

You should note that each engine implements different tweaks and techniques.