What is garbage collection

In JavaScript, the objects, strings…etc which require memory in a program execution are freed up when they are not in use. This cleaning up of memory is called garbage collection. JavaScript automatically frees up the memory location when not in use. Browser decides when to clean up data. Unsmooth rendering of a web-page sometimes, may be because of garbage collection. Although garbage collection is automatically done, we can adopt procedures to ease garbage collection in our code.

Below listed are suggestions how to help the garbage collection in JavaScript.

Object de-referencing

An object in JavaScript, is said to have a reference to another object if it can access the later object. De-referencing the objects, by assigning objects to null, is  useful in garbage collecting.

However, using variables in appropriate scope makes the variable out of scope when function is executed and variable is automatically garbage collected. This is preferred method over de-referencing.

This is very important in large applications or game applications where there are many objects or very less memory space. 

Syntax for de-referencing

var objectA = new Object();

//code to do some work

objectA = null; //De-referencing object

Object recycling

Structuring the code such that object once created can be re-used again, make efficient use of memory. Few simple steps are given below to reuse memory.

In arrays using [] creates a new array. We can reuse the same location by clearing the old data saying arr.length=0.

NOTE: Assigning the same array name to new one, garbage the old array and create new one.

If a specific function is called in setTimeout() repeatedly, it can be assigned to a variable and called, rather than making it anonymous function and spawn every time.

Example of object recycling

setTimeout(function{ //code here}, 10); //This code generates function each time it is called. Memory is utilized each time it is called.

var func = function{//code here}//This defines function only once in memory.

setTimeout(func, 10);

Mark and Sweep algorithm

In this mark and sweep algorithm, garbage collection happens in two phases: mark phase and sweep phase. In mark phase, the list of variables, objects and arrays are made. By recursively travelling through them all the variables which are referenced by them are marked. Any data not marked are ready to be sent to a garbage.

In sweep phase, all the values in the JavaScript environment that are not marked are de-allocated. This process of marking and sweeping occurs periodically.

When mark and sweep begins, a complete clean-up is done which slows down the program. However most modern browsers use sophisticated form of this algorithm, which is efficient and perform collection in background without interrupting performance.

Reference counting garbage collection

In this method, the references to an object (user created or built in HTML browser object) is counted. If the reference count is zero, then it is ready for garbage collection.

When an object is created and property is added, its reference count is 1.When we copy an object to other object, we have reference count as 2, as two objects have same reference. When one of the objects holding the reference is over-written by another value, the reference count becomes 1. If there are no references to the object and count reaches 0, browser understands that it is safe to destroy the object and free the space.

Example for reference counting type of garbage collection

var vehicle{

//Object created and variable is added to store its reference

name: “Maruti”

//Copy vehicle to object. Number of reference is 2.

var object = vehicle;

//Reference to property in vehicle is 1 i.e. object.

vehicle = otherObject;

//name has two reference now as -> person and property of object.

var person = object.name;

//name is only referenced by person.

vehicle = “areoplane”;

//The original vehicle object and its properties are not reference by any object after this step and can be garbage collected.

person = null;

Drawbacks of reference counting

Limitation of this method is if an object is referenced by another object and that object holds reference of the later, leads to cycling references. If both are not used in code, then the objects are never garbage collected.

However this method is not complex and is ideal for simple applications.

 

›› go to examples ››