[thelist] JS memory leak detection?

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Sun Oct 23 16:16:06 CDT 2005


Here are my observations on the leakage problem:

http://www.volkanozcelik.com/volkanozcelik/blog/2005/10/help-my-browser-leaks-memory.html

Looking at my tests, I see that the most recent. versions of IE and
Mozilla still experience memory leak problem, whereas Opera is pretty
good in garbage collection (no memory leak problems)

HTH,
Volkan.

2005/10/23, liorean <liorean at gmail.com>:
> On 10/23/05, aardvark <evolt at roselli.org> wrote:
> >
> > is attachEvent inherently a resource hog?
> >
>
> No. attachEvent behaves just fine and isn't inefficient.
>
>
>
> To return to iewleaking memory, the problem with leaks is that when you
> start referencing COM objects from JScript and JScript objects from COM
> objects iew cannot garbage collect thos objects properly. To show you an
> example of how easy it is to create such a reference, look at this:
>
>
> var
> coll=document.getElementsByTagName('td'),
> i=0,
> obj;
> while(obj=coll.item(i++))
> addSomeStupidListener(obj)
>
> function addSomeStupidListener(obj){
> obj.onmouseover=function(){
> alert(this);
> };
> }
>
>
>
> This code adds a circular reference to all table cells on the page. Where is
> the reference? Well, addSomeStupidListener contains a formal parameter obj,
> ie a local variable. The inner function closes over all local variables, ie
> it closes over the obj variable. Since this variable will contain a
> reference to the element the function is assigned to, this is a typical way
> of how circular references get created. JS-->COM-->JS-->COM-->...
>
> COM and JScript have separate garbage collectors that don't detect these
> kinds of circular references, so they can't clean up properly.
>
>
> Is there a way to solve this problem?
> Yes, there is. A slight change like this will solve it, for this specific
> case:
>
>
> var
> __reflist__=[],
> coll=document.getElementsByTagName('td'),
> i=0,
> obj;
> while(obj=coll.item(i++))
> addSomeStupidListener(obj)
>
> function addSomeStupidListener(obj){
> obj.onmouseover=function(){
> alert(this);
> };
> __reflist__.push(function(){obj=null});
> }
>
> window.onbeforeunload=function(){
> while(__reflist__.length>0)
> __reflist__.pop()();
> };
> --
> David "liorean" Andersson
> <uri:http://liorean.web-graphics.com/>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>


--
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