Memory life cycle

When executing assignment statement in JavaScript, JavaScript engine will allocate memory to store new given values.

for example:
const students = ['John', 'David', 'Susan'];

When running the above code, the JS Runtime will allocate a block of memory to store array ['John', 'David', 'Susan'].

and then we can change values of allocated memory with the following statement
students[0] = 'Bob';

Finally the allocated memory will be freed if it is no longer in use by not referencing to it anymore with the following statement.
students = [];

The memory which is allocated for array of ['John', 'David', 'Susan'] will be freed after assigning [] to students.

So there are 3 steps in processing memory management.

  • Allocated memory for values
  • Read/write allocated memory
  • Release unused memory

Allocation memory

  1. For primitive values, memory will be allocated in stack memory.
    const count = 0;
  2. For complex values such as Object, Array, Date,..., memory will be allocated in heap memory.
    const todos = ['fix', 'unit-test', 'commit'];

Read/write allocated memory

Memory after being allocated can be read or written. Let take a look at the example below.

// a block of memory will be allocated for new array
const todos = ['fix', 'unit-test', 'commit'];  
// Print out the first values of todos array.
console.log(todos[0]);  
// We add more item to todos array.
todos.push('build');  

Release unused values

When the allocated memory is not needed any longer, it will be freed and used for other new values. But the process to determine when the allocated memory is not needed any longer is complicated.

High-level language implement special mechanism called garbage collection to track memory allocation and use in order to find when the allocated memory is not needed any longer

Garbage Collection

References

function Student(name, dob) {  
  this.name = name;
  this.dob = dob;
}
let bob = new Student('Bob', new Date('2009-01-01'));  

Object Reference Diagram Figure 1: Object Reference Diagram

The expression new Student('Bob', new Date('2009-01-01')); create new object which is being held by bob variable. bob has a property dob referencing object Date created by the expression new Date('2009-01-01').

Circular reference
function Student(name, dob) {  
  this.name = name;
  this.dob = dob;
  this.circularReference = this;
}
let bob = new Student('Bob', new Date('2009-01-01'));  

Figure 2: Circular Reference

bob has a property circularReference that pointing to itself.

Reference-counting garbage collection

An object is not needed anymore when there is no other object referencing to it. In other word, an object is considered garbage collectable if there is zero reference pointing at this object.

Programmer must explicitly remove any reference to unused objects to make it garbage collectable.

Wrap up

In low-level language like C, we have to do allocating memory and freeing memory explicitly. It mean developer have to manage memory allocation by themselves.

In high-level language like JavaScript, Java, programmer don't need to do this task. The Runtime will manage memory by itself. But the bottom line is to avoid keeping reference to unused objects.

  • Circle reference on browser is not problem anymore. This kind of problem has been resolved by most of modern browser.

  • Objects need to be made explicitly unreachable