I have modified my test classes slightly. And found that when debugging this
movie in Flex Builder 3, the garbage collection was triggered when I moved
my mouse within the Flash movie.

I suspect that the method behind the screens that runs the garbage collector
is quite complex. I think that in more complex applications garbage
collection is more efficient.


Counter.as

package
{
   import flash.display.BitmapData;

   public class Counter
   {
      public var bmd:BitmapData;

      public function Counter()
      {
         bmd = new BitmapData(500, 500);
         bmd.noise(Math.random() * 1000000);
      };
   };
};


SpriteClass.as

package
{
   import flash.display.Sprite;

   public class SpriteClass extends Sprite
   {
      public function SpriteClass()
      {
      };
   };
};


testAS.as

package
{
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.text.TextField;

   [SWF(frameRate="4")]
   public class testAS extends Sprite
   {
      private var _t:TextField;
      private var _c:uint;
      private var _a:Array;

      public function testAS()
      {
         _a = new Array();

         _t = new TextField();
         addChild(_t);

         var a:SpriteClass = new SpriteClass();
         a.addEventListener(Event.ENTER_FRAME, _handler);
      };

      private function _handler(e:Event):void
      {
         _t.text = _c.toString();

         _a.push(new Counter());

         _c++;

         if (_c > 300)
         {
            (e.currentTarget as SpriteClass).removeEventListener(
Event.ENTER_FRAME, _handler);
         };
      };
   };
};



Greetz Erik




On 3/14/08, EECOLOR <[EMAIL PROTECTED]> wrote:
>
> Hey there,
>
> I was totally shocked this morning. If what you were saying was true it
> would mean that if you created a class with a listener to itself and
> instantiated it, it would never be garbage collected.
>
> So I did some extra tests.
>
> 1. Your example with a weak listener (same result)
> 2. Trying to force garbage collection using System.gc and the
> localconnection 'hack' (same result)
>
> So I turned to the Flex
> Profiler. I used the following class to test things:
>
> package
> {
>    import flash.display.Sprite;
>    import flash.events.Event;
>    import flash.system.System;
>    import flash.text.TextField;
>
>    [SWF(frameRate="1")]
>    public class testAS extends Sprite
>    {
>       private var _t:TextField;
>       private var _c:uint;
>       private var _a:Array;
>
>       public function testAS()
>       {
>          _a = new Array();
>
>          _t = new TextField();
>          addChild(_t);
>
>          var a:SpriteClass = new SpriteClass();
>          a.addEventListener(Event.ENTER_FRAME, _handler);
>       };
>
>       private function _handler(e:Event):void
>       {
>          _t.text = (_c++).toString();
>          _a.push(new Counter());
>       };
>    };
> };
>
> Initialy the profiler showed the behaviour you were describing. However,
> if I pressed
> the "Run Garbage Collector" button, the instance of SpriteClass was removed. 
> This indicates the behaviour that I described in my example.
>
>
> I changed the above example to use a weak listener. The instance of 
> SpriteClass was only removed after I clicked the "Run garbage collector" 
> button.
>
> In the handler I removed the listener to the SpriteClass instance. The
> instance of SpriteClass was only removed from memory after I clicked
> the "Run garbage collector" button.
>
> If I run the profiler with the
> option "Generate object allocation stack traces", the instance is removed 
> everytime quite fast.
>
>
> The conclusion of all this is that my statement in previous emails was
> correct. What your example shows is that while the instance _can_ be garbage
> collected, you will never know when it actually will.
>
>
> A summary:
>
> - There is no such thing as "a reference to be created invisibly within
> the event system".
> - You can not be sure when an instance is garbage collected.
> - Weak references only indicate that: if the reference on the dispatcher
> object to the listener is the last one, the instance containing the listener
> will me available for garbage collection.
>
>
> I want to thank you Cory for being as persistent as I can be. It enabled
> me to do extensive tests to see
> how the player works. In my tests in a standalone player there were cases 
> where I let the example application run for more then half an hour without 
> garbage collection of SpriteClass. I am not sure when the garbage collector 
> kicks in (might be triggered by total memory usage). I will continue to 
> monitor the
> example
> to see if the instance will ever be garbage collected. If not, I will file a 
> bug at adobe.
>
>
>
> Greetz Erik
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to