1 Reply Latest reply on Dec 12, 2010 9:08 PM by celebritymusic

    Memory experiment - why does this happen?

    celebritymusic Level 1

      Hi

       

      I've borrowed code from a couple of blogs to do some memory leak tests.  Copy the code at the end of the post into Flash, and create a symbol with a library linkage name of BoxMC.  Delete the symbol off the stage.

       

      The code does three things...

       

      1) when you click the stage, the box disappears.

       

      2) a garbage collection is forced

       

      3) the system memory usage gets traced every two seconds.

       

       

      Run it, let Flash settle down for a couple of seconds, and then take note of the memory traced.

       

      Now click to make the box disappear.

       

      For me, the memory increases after the garbage collection.

       

      Why is this?  Shouldn't it decrease?

       

       

       

      var box:MovieClip = new BoxMC();
      box.x = box.y = 100;
      addChild(box);

       

      stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);

       

      function onClick(evt:MouseEvent):void {
      this.removeChild(box);                                 //ball removed from display list but still exists
      trace(box)
      //trace(["box"].y);
      box= null; //ball now entirely removed
      trace(box)
      stage.removeEventListener(MouseEvent.CLICK, onClick);
      }

       


      import flash.system.System;

       

      // check our memory every 1 second:
      var checkMemoryIntervalID:uint = setInterval(checkMemoryUsage,2000);

       

      function checkMemoryUsage() {
      trace (System.totalMemory)

       

      try {
      new LocalConnection().connect('foo');
      new LocalConnection().connect('foo');
      } catch (e:*) {}

       


      }