Search code examples
javascriptobjectdestroy

How to destroy a JavaScript object?


Recently, I came across one of my application which consumes too much memory and increasing by 10 MB/sec.

So, I like to know how to destroy JavaScript object and variables so memory consumption stays down and my FF can't get destroyed.

I am calling two of my scripts every 8 seconds without reloading the page.

function refresh() {
    $('#table_info').remove();
    $('#table').hide();
    if (refreshTimer) {
        clearTimeout(refreshTimer);
        refreshTimer = null ;
    }
    document.getElementById('refresh_topology').disabled=true; 
    $('<div id="preload_xml"></div>').html('<img src="pic/dataload.gif" alt="loading data" /><h3>Loading Data...</h3>').prependTo($("#td_123"));
    $("#topo").hide();
    $('#root').remove();
    show_topology();
}

How can I see which variable cause Memory overhead, what's the method to stop the execution of that process?


Solution

  • You could put all of your code under one namespace like this:

    var namespace = {};
    
    namespace.someClassObj = {};
    
    delete namespace.someClassObj;
    

    Using the delete keyword will delete the reference to the property, but on the low level the JavaScript garbage collector (GC) will get more information about which objects to be reclaimed.

    You could also use Chrome Developer Tools to get a memory profile of your app, and which objects in your app are needing to be scaled down.