views:

34

answers:

2

My AS3 application basically does the following (pseudo code from main application class) 3 or 4 times by adding custom objects to stage:

_movieClipClassVariable = new MyCustomSpriteSubclass();     
_movieClipClassVariable.addEventListener(MyEvents.READY, function(event:Event):void {
            _hideLoading();
            mcHolder.addChild(_movieClipClassVariable);                               
        });                     

_movieClipClassVariable.addEventListener(MouseEvent.CLICK, myClickHandler); 


private function coverClickHandler(event:Event):void
{           
    ...
}

What is the right way to allow Garbage Collector to recycle _movieClipClassVariable after it is not necessary? Assign null to it? Remove all listeners? Use weak reference for listeners?

Thanks in advance!

+1  A: 

I would say all of the above.

I recommend reading Grant Skinners articles of Resource Management. Also take a look at his slides from his Resource management talk.

There is quite a lot of information out there on this subject, and those two links are the best resources I have found.

TandemAdam
A: 

In order to get use of garbage collector you should consider:

  1. Not defining handler methods inside the X.addEventListener() call
  2. Remove all event listeners on the object you want to free up from memory
  3. Make the object null 4.(optional) you can force garbage collector calling system.gc();
Adrian Pirvulescu
Is removing listeners literally a better way than using weakly referenced listeners?http://www.gskinner.com/blog/archives/2006/07/as3_weakly_refe.html
artvolk
not "literally" (because that doesn't make grammatical sense). But it is always better/safer to manually remove your listeners, rather than being lazy and relying on weak references. I can't hurt to use both though.
TandemAdam