1

Possible Duplicate:
jQuery memory leak with DOM removal

I created a simple javascript code snippet to test memory leak.

function createDom(howmany)
{
    var i;
    var el;
    var body = $("body");
    for(i=0;i<howmany;i++)
    {
        el = $("<div></div>");
        el.text(i);
        el.addClass('element');
        body.append(el);
    }
}

$(document).ready(function(){

    createDom(10000)

});

when i opened the file in Chrome, it uses about 20mb. When I move to a new url (about:blank), Chrome clears up the memory.

The problem is that Chrome uses more memory when I execute $(".element").remove(). The bigger problem is that the memory stays even if I change the URL.

Am I testing it incorrectly? I almost feel like I should just hide all the element instead of removing them...

Community
  • 1
  • 1
Moon
  • 22,195
  • 68
  • 188
  • 269

1 Answers1

0

Thought this might be of some help , so posted it. Methods remove(), empty() and html() check descendant elements for data and clean them. That’s the overhead, but, at least, the memory can be reclaimed. Try using removeData() and then detach(). Hope it will help in some way.

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • // when I use native javascript to remove those elements, the memory doesn't increase, but remains pretty much the same. What do you mean by "can be reclaimed"? Should I call a function to free up the memory? – Moon Jan 12 '12 at 21:53