![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Hi, Douglas Crockford mentioned in a video on Yahoo! that before removing an element it is a good idea to purge it's event handlers. I think he was only refering to the event handlers defined inline in the HTML element attributes. This purging is because the event handlers create the circular memory leak in IE. I am trying to create and observe this memory leak with with IE6 and the example below. I can't see any memory leaking. |
{
#2
| |||
| |||
|
|
Peter Michaux wrote: Hi, Douglas Crockford mentioned in a video on Yahoo! that before removing an element it is a good idea to purge it's event handlers. I think he was only refering to the event handlers defined inline in the HTML element attributes. This purging is because the event handlers create the circular memory leak in IE. I am trying to create and observe this memory leak with with IE6 and the example below. I can't see any memory leaking. I changed my test to the following to try to force the circular leak. More memory definitely is used but during the test IE does seem to garbage collect at times. function doLoad() { for (var i=1000000;i-- {var p = document.createElement("p"); p.appendChild(document.createTextNode("clickme")); p["onclick"]=(function(p){ return function(){alert("hi");}; })(p); document.body.appendChild(p); document.body.removeChild(p); } } |
#3
| |||
| |||
|
|
I am trying to create and observe this memory leak with with IE6 and the example below. I can't see any memory leaking. |
|
I changed my test to the following to try to force the circular leak. More memory definitely is used but during the test IE does seem to garbage collect at times. function doLoad() { for (var i=1000000;i-- {var p = document.createElement("p"); |
|
p.appendChild(document.createTextNode("clickme")); p["onclick"]=(function(p){ return function(){alert("hi");}; })(p); document.body.appendChild(p); document.body.removeChild(p); } } |
#4
| |||
| |||
|
|
Peter Michaux wrote: [...] I changed my test to the following to try to force the circular leak. More memory definitely is used but during the test IE does seem to garbage collect at times. function doLoad() { for (var i=1000000;i-- {var p = document.createElement("p"); p.appendChild(document.createTextNode("clickme")); p["onclick"]=(function(p){ return function(){alert("hi");}; })(p); document.body.appendChild(p); document.body.removeChild(p); } } It looks like I was not interpreting the task manager output correctly. The virtual memory usage was going crazy with this example. Looks like Crockford was right as I expected. |
{
#5
| |||
| |||
|
|
If you want an even better demonstration (inspired by a Richard Cornford post) head over to <URL:http://www.lipsum.com/> and generate say 50 paragraphs of Ipsum lorem (should be about 30k of text). Paste that into a div called 'xx' and modify your script to be: function doLoad() { for (var i=10000;i-- {var p = document.createElement("p"); var x = document.getElementById('xx').innerHTML; p.appendChild(document.createTextNode("clickme")); p["onclick"]= (function(p, x){return function(){alert("hi");};})(p, x); document.body.appendChild(p); document.body.removeChild(p); } } For me it caused IE to swallow about 500MB of RAM each time the page was loaded (after 2 you lose 1GB of RAM). It stays lost until IE is closed. Note that I reduced the number of loops to 10,000. |
#6
| |||
| |||
|
|
Thanks for the example and info. This closure and IE leak stuff is starting to make more sense now. |
![]() |
| Thread Tools | |
| Display Modes | |
| |