[Greasemonkey] Re: GM & FF memory leaks?

Andre gm at andrecgn.de
Tue Feb 7 23:27:06 EST 2006


I have been experiencing the same problems. Somebody suggested to remove 
  the EventListeners on "unload". Here is the code:


EventManager= {
   _registry: null,
   Initialise: function() {
     if (this._registry == null) {
       this._registry = [];
       EventManager.Add(window, "_unload", this.CleanUp);
     }
   },
   Add: function(obj, type, fn, useCapture) {
     this.Initialise();
     if (typeof obj == "string")
       obj = document.getElementById(obj);
     if (obj == null || fn == null)
       return false;
     if (type=="unload") {
         // call later when CleanUp is called. don't hook up
         this._registry.push({obj:obj, type:type, fn:fn, 
useCapture:useCapture});
         return true
     }
     var realType=(type=="_unload"?"unload":type);
     obj.addEventListener(realType, fn, useCapture);
     this._registry.push({obj:obj, type:type, fn:fn, 
useCapture:useCapture});
     return true;
   },
   CleanUp: function() {
     for (var i = 0; i < EventManager._registry.length; i++) {
       with(EventManager._registry[i]) {
         if(type=="unload") {
             fn();
         } else {
             if (type="_unload") type = "unload";
             obj.removeEventListener(type,fn,useCapture);
         }
       }
     }
     EventManager._registry = null;
   }
};

attaching an EventListener works like this:

     EventManager.Add(trigger, "click",  myOnclick,  false);
     EventManager.Add(trigger, "unload", myOnunload, false);

Andre



Tom Van Herreweghe wrote:
> I'm currently creating a userscript that implements some sort of instant 
> messenger. I created some froms, and display them on the site.
> The UI reloads every 3 seconds (yeah, that's quite fast). I noticed FF 
> crashed a lot when the userscript was enabled.
> 
> I tried to debug it with the javascript console. I succeeded in killing 
> all the bugs, except one (but that's not what's causing the leak).
> I'm attaching a lot of events to the UI to check for user interaction: 
> reading messages, opening/closing new windows, ...
> 
> After a good search on google, I found that "addEventListener" causes 
> minor memory leaks, even on FF. I checked if it was true, and yes, 
> according to the task manager, FF went from 25Mb to about 38Mb 
> (gradually). I then found a replacement function to change the 
> "addEventListener" in order to minimise memory leaks:
> 
> function addEvent( obj, type, fn )
> {
>    if (obj.addEventListener)
>        obj.addEventListener( type, fn, false );
>    else if (obj.attachEvent)
>    {
>        obj["e"+type+fn] = fn;
>        obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
>        obj.attachEvent( "on"+type, obj[type+fn] );
>    }
> }
> 
> Now, FF seemed to crash a lot slower then before, but still, it crashes. 
> I checked the memory usage again, and it went from 25Mb to about 32Mb. 
> The strange thing is, when only one tab is open, and I just browse the 
> internet, it takes quite a long time for FF to crash. As soon as I start 
> posting on a forum, or opening multiple tabs, FF crashes quite fast.
> 
> Does anyone have any idea about how to fix memory leakage for GM + FF ? 
> I have searched for many days now, tried a lot of different code, ... I 
> feel like making a nice project from this userscript, but right now, all 
> the FF crashes make me feel depressed.
> 
> The full script can be downloaded here: 
> http://hermod.encapsulated.org/wb/index.htm  (user scrip 0.3 alpha)
> 
> Thanks for your input
> 
> Tom



More information about the Greasemonkey mailing list