4 Replies Latest reply on Jun 5, 2008 11:25 PM by charithao

    Memory leak with Timer

    charithao
      Hi,

      I have a problem using the Timer in Flex. I am using a timer instead of settimeout function because settimeout sometimes cause memory leaks. In the COMPLETE event of the timer a big display object with videos and images on it is created and added to the stage. Before this any previous ones will be removed. And then timer is started again. Some times the big display objects are not removed from the memory. The interval of the timer is rather shorts. It's about 2 seconds which is not sufficient to load the movies and images in the page. This is done on purpose to test this application. After using the profiler I noticed that timer is keeping a reference to the object. How could I avoid this? I couldn’t provide any source code because I am not allowed to do so. Any help will be appreciated.

      Thanks
        • 1. Re: Memory leak with Timer
          atta707 Level 2
          Can you some short but complete code that's causing problem?
          • 2. Re: Memory leak with Timer
            slaingod Level 1
            You must always call removeEventListener if you explicitly call addEventListener or you will generate a memory leak...not saying that is your issue.
            • 3. Re: Memory leak with Timer
              Garyl Woolworth Level 1
              Also if you are checking the profiler right off the bat with the timer you may not be waiting long enough for the GarbageCollector to run. This is also assuming that you have removed all references to the object so that the GC itself will even pick it up.
              • 4. Re: Memory leak with Timer
                charithao Level 1
                This is a strip down vesrion of the class to get a basic idea. Actually I tried a forced GC but the ItemDisplay objects remain in the memory but not all the objects. For example if 100 instances of the ItemDisplay objects are created few of them remain on memory while others get cleaned up.

                public class MyClass extends Canvas
                {

                private var timer:Timer = new Timer(0,1);
                private var _itemlist:ItemList;
                private var currentItem:ItemDisplay;
                private var currentIndex:Number = -1;


                [Bindable]
                public function set itemList(pItemList:ItemList):void
                {
                _itemlist = pItemList;
                currentIndex = -1;
                if (pItemList != null){
                stop();
                stepNext();
                }else{
                clear();
                }
                }


                public function MyClass()
                {
                timer.addEventListener(TimerEvent.TIMER, stepNext);
                }


                public function stop():void
                {
                timer.stop();
                currentItem = null;
                }

                public function clear():void
                {
                stop();
                removeAllChildren();
                }

                private function stepNext(pEvt:Event=null):void
                {
                stop();
                if (_itemlist.Items.length > 1)
                {
                var pos:Number = (currentIndex < _itemlist.Items.length-1) ? currentIndex + 1 : 0;
                displayItem(pos);
                }
                else if (_itemlist.Items.length == 1)
                {
                if (pEvt == null) displayItem(0);
                }
                timer.reset();
                timer.delay = 2000;
                timer.start();
                }

                private function displayItem(num:Number = 0):void
                {
                currentIndex = num;

                currentItem = null;

                removeAllChildren();
                currentItem = new ItemDisplay();
                currentItem.setItemList(_itemlist.Items
                );
                addChild(currentItem);

                }
                }
                }