Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Reference-counting garbage collection in Javascript

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 906
    Comment on it

    In Javascript, when we define any variable(string, object, integer, function etc..), some memory is allocated with it and freed when they are not in use by garbage collector and this process is called garbage collection. This whole process we can define as memory life cycle:


    1. First is to allocate memory
    2. Second is to use this memory as executable operation
    3. last is to free the memory that was used

    Allocate memory

    allocation can be done by declaring a variable, function or object:

    var a = 1, b = abcd; // allocating a number, string
    var obj = {prop1:ex1, prop2:1} // allocate an object
    var fun = function(param){
        this.param = param;
    } // allocate a callable object
    var arr = [apple,mango,1,2] //allocate an array
    

    Release memory

    Releasing the memory is a job of garbage collector which automatically free the memory that is no longer used by any reference.This is the general problem of knowing whether the memory is needed or not. For solving this problem we use some garbage collection algorithm. These algorithm finds the references and based on it algorithm decides whether to select object as garbage or not.


    Reference counting Algorithm

    This algorithm uses the concept of counting the number of references on an object, if it finds any variable having 0 reference on it algorithm selects it for garbage collection.

    The mechanism which is used to make the reference 0 is:

    1. When we create an object and store this in another variable its reference count is incremented to one, similarly when its reference is use in another variable or function its reference count will also be increased to two.

    2. If we assign some another value to variable that was using objects reference then reference count will decrease to one. Same can be done with another variable which is using objects reference.

    3. Now object is not using by any other reference, so javascript can destroy it and release its associated memory.


    Example

    var a = {obj:{name:"my_name"}};
    var b = a;
    a = 1;
    //Here we created an object, which is used by variable a and b
    // we have 1 to a so one of objects reference is reduced to 1
    // still we have one reference which is b
    b = null;
    // now object has not any reference left, garbage collector will take this object.
    

    Reference counting algo has a disadvantage of dealing with cyclic reference. When object A has a reference of object B and B has a reference of A then garbage collector will never find these object free.


    var a = {};
    var b = {};
    a.x = b;
    b.y = a;
    

    In above condition garbage collector will not find anyone of object. For overcome to this situation most of browser support Mark-and-sweep algorithm, which uses concept of finding those object that are not in use for longer time rather to find object that has 0 reference.



 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: