Memory Management in Javascript | Memory life cycle

Unlike Low-Level Programming Language C, JavaScript does not have alloc(), malloc(), free() primitive to do a job for them. Memory is automatically freed when that is no longer useful. 


This process is called Garbage Collection. Word Automatically brings confusion to developers, that they need not worry about Memory, And this is a mistake, Sir! 


In memory management, its life cycle

  1. Allocate Memory(explicit or implicit depending on a programming language for us it is implicit)
  2. Use it.
  3. Release it.

This is a general life cycle.


Allocation Of Memory in JavaScript for memory management

When values are assigned to any variable memory is assigned to it.


var a = 10;

var b = "CronJ";

var c = {
   d : 1,
   e: 'name'

var f = [1,2,3,4,5]

var g = new Date();

Release when the memory is not needed anymore

This is the climax scene. When? how? to decide memory is no longer useful. High-Level Programming Language like Javascript has some software called Garbage Collector, which does the job of automatic memory deallocation. This is the approximation Process. Exact point from memory management which that memory is not gone any benefits to an application is not solved yet.


Garbage Collector

Finding the exact point from which memory will be no longer useful is not solved yet. This is the general problem any Programming language face.


Reference-counting garbage collection:

Any variable which has zero reference points can be considered to garbage.

Let’s take an example. Taken Mozilla developer Guide.


var o = { 
  a: {
    b: 2
// 2 objects are created. One is referenced by the other as one of its properties.
// The other is referenced by virtue of being assigned to the 'o' variable.
// Obviously, none can be garbage-collected

var o2 = o; // the 'o2' variable is the second thing that 
            // has a reference to the object
o = 1;      // now, the object that was originally in 'o' has a unique reference
            // embodied by the 'o2' variable

var oa = o2.a; // reference to 'a' property of the object.
               // This object has now 2 references: one as a property, 
               // the other as the 'oa' variable

o2 = 'yo'; // The object that was originally in 'o' has now zero
           // references to it. It can be garbage-collected.
           // However its 'a' property is still referenced by 
           // the 'oa' variable, so it cannot be freed

oa = null; // The 'a' property of the object originally in o 
           // has zero references to it. It can be garbage collected.


Limitation: cycles

There are limitations to it. If two objects are referencing to each other then they will be thrown out of the scope of garbage collection.


var a = {};

var b = {};

a.c = b;

b.c = a;


Although JavaScript has software called Garbage Collector. We have to take care that we are not causing circular referencing to variables. We are not coding anything which will lead to throwing variables from out of Garbage collection Cycle.

Be it a software developer, programmer, coder, or a consultant, CronJ has it all. CronJ has been a trustworthy company for startups, small companies, and large enterprises. Hire the web of experienced ReactJS Development Services for your esteemed project today.

Published by sasi kiran


Reply heres...

Login / Sign up for adding comments.