Garbage Collection in JavaScript

Rich application development now a days includes wast use of JavaScript and Ajax. Latest web browsers are developed to handle such interactive web applications. All data which Ajax imports from server is stored in web browser memory cache. Larger amount of data stored in web browser cache may slow down performance of web browser. One way to release memory from web browser cache or applying garbage collection is to close the browser. But web application can't use this practice to close browser or tell user to restart web browser while web application application is running.

You can force/implement garbage collection in JavaScript. Web developers should develop practice to force garbage collection contentiously while web application is running.


delete operator is one of the most unknown and unused operators in JavaScript. You can use delete operator to release memory occupied by any unused or unreferenced objects in JavaScript. While deleting any object be sure you don't have any reference stored in any other variables for particular object. This will lead performance breakdown when these other variable will try to access the object which was deleted. You can delete any object which has performed it's job and is not going to be used in future.

For example, You can delete window onload handler function and release any memory associated with the function, like this:

var winLoadFun = function(){
    
        // code for my function
        delete winLoadFun;
    }

    window.addEventListener('load', winLoadFun , false);


Comments