Skip to content

Instantly share code, notes, and snippets.

@aarongustafson
Created December 5, 2009 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarongustafson/249828 to your computer and use it in GitHub Desktop.
Save aarongustafson/249828 to your computer and use it in GitHub Desktop.
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