Quick tip: Timer Listeners Doesn’t Require Remove

Based on profiler (or net.hires.debug.Stats) your Timer listeners doesn’t need to be removed in order to empty garbage collector – the release reference, however it is always a good practice to remove all of them, event those with anonymous functions. Quick proof of concept:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    import net.hires.debug.Stats;
    
    [SWF(frameRate="60")]
    
    public class WonderflApp extends Sprite
    {
        public function WonderflApp()
        {
            addChild(new Stats());
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        public function loop(event:Event):void
        {
            for(var i:uint = 1; i < 100; i++)
            {
                var timer:Timer = new Timer(100, 1);
                timer.addEventListener(TimerEvent.TIMER_COMPLETE, 
                    function onTimer(event:TimerEvent):void{});
                timer.start();
            }
        } 
    }
}

Simplest way to remove anonymous function listeners:

EventDispatcher(event.currentTarget).removeEventListener(event.type, arguments.callee);

Leave a comment

Please be polite and on topic. Your e-mail will never be published.