Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of a memory leak issue in IE and how to resolve it
window.onload = function()
{
var
body = document.getElementsByTagName('body')[0],
div = document.createElement('div'),
parentDiv, childDiv;
for ( i = 0; i < 5000; i++ )
{
parentDiv = div.cloneNode(true);
parentDiv.onclick = foo;
childDiv = div.cloneNode(true);
childDiv.onclick = bar;
// Doesn't Leak
body.appendChild(parentDiv);
parentDiv.appendChild(childDiv);
body.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
body = null;
}
window.onload = function()
{
var
body = document.getElementsByTagName('body')[0],
div = document.createElement('div'),
parentDiv, childDiv;
for ( i = 0; i < 5000; i++ )
{
parentDiv = div.cloneNode(true);
parentDiv.onclick = foo;
childDiv = div.cloneNode(true);
childDiv.onclick = bar;
// Leaks
parentDiv.appendChild(childDiv);
body.appendChild(parentDiv);
body.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
body = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.