[thelist] JS memory leak detection?

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Mon Oct 24 04:15:36 CDT 2005


One final notice:
IE's problem is not 100% related to circular references (using
circular references just makes the issue more apparent)

Here is a sample with no circular refs:

<html><head>
<meta http-equiv="refresh" content="1;" />
<script type="text/javascript">
window.onload=function(evt){
var element = document.getElementById("my-element");
element.attachEvent("onclick",function(){});
element.bigString=new Array(1000).join(new Array(200).join("XXXXX"));
};
</script>
</head><body>
<div id="my-element"></div>
</body></html>

There is no circular reference to "my-element" or "element" in the
code. However IE still fails to clean up memory properly on the long
run (in 10+ minutes)

The same code replacing attachEvent with addEventListener

element.addEventListener("click", funciton(){}, false);

works without leak in Mozilla.

However if we alter the code a bit:

<html>
<head>
<meta http-equiv="refresh" content="1;" />
<script type="text/javascript">
window.onload=function(evt){
var element = document.getElementById("my-element");
var fnc=function(){var ele=document.getElementById("my-element2");};
element.addEventListener("click", fnc, false);
/*Make sure we see the leak*/
element.bigString = new Array(1000).join(new Array(200).join("XXXXX"));
};
</script>
</head><body>
<div id="my-element">click me</div>
<div id="my-element2">click me</div>
</body></html>

Altough there is no circular reference to "my-element" (the function
is refering to node "my-element2" not "my-element") Mozilla shows some
leak (much less than IE but it leaks anyway) on the long run as well.

Cheers,
Volkan.

2005/10/24, VOLKAN ÖZÇELİK <volkan.ozcelik at gmail.com>:
> > >
> > > Just a note about that code you have there: The unload event isn't reliable
> > > for a number of reasons. Especially not in op. I suggest using
> > > onbeforeunload instead.
> >
>
> Here are compatibility results for onbeforeunload:
>
> Opera 8 -> *it does not work*
> IE -> works okay
> Moz -> works okay
> Saf -> not tested
>
> So voting for onbeforeunload against onunload because Opera does not
> support ununload is kinda mistake. Because Opera does not support
> onbeforeunload at all.
>
> Here is what ppk says about Opera' onunload:
>
> "
> In Opera load and unload do not fire when the user uses Back or
> Forward to enter or leave a page, or when the user closes the window.
> "
> ... onload support of opera is similar. again PPK says:
>
> "
> Opera itself says this is the correct way to handle onload. Although
> this may be true, it will certainly cause compatibility problems.
> "
>
> Here is another quote from someone else:
>
> "IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, and Firefox support onbeforeunload"
>
> At least Safari fully supports onunload event. I am not sure whether
> it supports onbeforeunload, and if it does whether it supports it
> correctly.
>
> Having seen that Opera does not have the memory leakage problem at
> all, the onunload fix is basically for ie and moz.
>
> In short, I think I'll stick with onunload.
>
> Or I may use a switch like:
>
> var g_bul_fired=false;
> window.onbeforeunload=function(evt){
>  g_bul_fired=true;
>  ... cleanup ...
> };
> window.onunload=function(evt){
>  if(!g_bul_fired){
>    ... cleanup ...
>  }
> };
>
> Cheers,
> --
> Volkan Ozcelik
> +>Yep! I'm blogging! : http://www.volkanozcelik.com/volkanozcelik/blog/
> +> My projects/studies/trials/errors : http://www.sarmal.com/
>


--
Volkan Ozcelik
+>Yep! I'm blogging! : http://www.volkanozcelik.com/volkanozcelik/blog/
+> My projects/studies/trials/errors : http://www.sarmal.com/


More information about the thelist mailing list