Unloading SWFs: Loader.unload() versus Loader.unloadAndStop() Methods

In this part of our experiment, we use container_3.fla, ext_docclass.fla, ThreeCircles.as, and ext_docclass.swf. Through tracing, we demonstrate that the simple Loader.unload() method deos not remove listeners attached to 'stage' by a loaded SWF nor it removes listeners to events like ENTER_FRAME even though they may be attached to an instance of a loaded SWF not to 'stage'.

It is very difficult to remove a loaded SWF from memory after it is unloaded via Loader.unload() method. Many event listeners contained in an unloaded SWF persist and make the content inelligible for garbage collection.

In the files contained in the swfsandstage.zip package, we use 'trace' calls to demonstrate that the listener 'whenClicked' attached to 'stage' by ext_docclass.swf (see the code for ThreeCircles.as on the previous page) persists as well as the listener to ENTER_FRAME even though the listener to ENTER_FRAME is attached to the instance of the loaded SWF and not to 'stage'.

 

package {

...........................

public class ThreeCircles extends MovieClip {

...........................

//We are adding a new listener to the constructor.

public function ThreeCircles(){

...........................

this.addEventListener(Event.ENTER_FRAME,threeEnter);

}

//End of the constructor.

...........................

private function threeEnter(e:Event):void {

trace("ENTER_FRAME listener from ThreeCircles is running.");

}

...........................

//We are adding a 'trace' call to 'whenClicked' listener.

private function whenClicked(e:MouseEvent):void {

renderCircles();

trace("Stage listener to CLICK from ThreeCircles is running.");

}

...........................

}

}

In container_3.fla we add a button btnUnload with the following listener:

 

btnUnload.addEventListener(MouseEvent.CLICK,unloadSwf);

 

function unloadSwf(e:MouseEvent):void {

loader.unload();

//loader.unloadAndStop();

mcExternal=null;

mcExplanations.visible=true;

}

 

(mcExplantions is a MovieClip that appears after the Unload button is clicked.) The commented out method loader.unloadAndStop() is not available in Player 9. We test container_3.fla and see through 'trace' calls that the listeners are active even after the loader.unload() was called.

Now the good news!  In Flash CS4 and Flash Player 10 there is a new ActionScript method available 'Loader.unloadAndStop()'. We use it in the file cs4container_3.fla in the package instead of 'loader.unload()'. The difference is huge. All the unwanted listeners are removed and it seems that our SWF is garbage-collected.

According to the AS3 current documentation, after calling Loader.unloadAndStop, the following happens:

  • Sounds are stopped.
  • Stage event listeners are removed.
  • Event listeners for enterFrame, frameConstructed, exitFrame, activate and deactivate are removed.
  • Timers are stopped.
  • Camera and Microphone instances are detached
  • Movie clips are stopped.

The method is also supposed to prompt the garbage collector to remove the unloaded content from memory. It does seem to work although we plan to run addtional tests with large SWFs containing media files.

Download

Download all the 'fla' and 'as' files corresponding to the isues discussed in this tutorial in one zip file.

When writing this tutorial, we found 'Essential ActionScript 3.0' by Colin Moock essential. This truly great book explains subtle aspects of loading which would take weeks to discover by experimentation.

Back to Intermediate Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.