Unload vs UnloadAndstop

May 7th, 2010 by aabhaanjan

I had a SWfLoadersClass for a large application which loads a bunch of external swfs, and when requested distributes copies of those swfs to the part of application. The application was developed in

Flash CS3. When I was using Loader.unload() method of Flash Player 9 I found it was very difficult to remove a loaded SWF from memory after it is unloaded . Most fired Events were still running and within

15 minutes my application hanged.

How I knew the Events were still running after unload ?

package {

import flash.display.*;

import flash.net.URLRequest;

public class loadSWF  extends MovieClip {

private  var ldr:Loader = new Loader();

private function loadSWF(){

     var url:String = "http://www.unknown.example.com/content.swf";
     var urlReq:URLRequest = new URLRequest(url);


btnUnload.addEventListener(MouseEvent.CLICK, unloadButtonClicked);


private function traceEnterFrameLisnerIsWorking (e:Event):void {

trace(“ENTER_FRAME is running.”);


private function unloadButtonClicked(e:MouseEvent):void {





I found ‘trace’ calls that the listeners were still  active even after the loader.unload() was called.

After some Googling J I found The Loader.unload() method doesn’t do much. In that case Garbage

Collector is not working properly.

I found some other facts too :

A SWF file that has a music track is loaded into an application. Later, the SWF is unloaded using Loader.unload(). Though the SWF will be removed from the screen, the music will still be heard.

Solution :

In Flash Player 10, there is a new method, Loader.unloadAndStop(). The latter method stops execution of the left-over listeners and prompts the garbage collector to remove the external swf file from memory.

Loader.unloadAndStop() method is supposed to trigger garbage collector to remove an unloaded swf file from memory.

The unloadAndStop() method tries to solve this by stopping all actions running in a loaded element and clearing the element out of RAM. The unloadAndStop() methos belongs to the Loader class. The idea is to call this method on your Loader instance to clean what you have loaded.

What unlaodAndStop() does ? :

  • Stops all MovieClips
  • Stops all sounds playing/streaming
  • Stop/removes all Timer objects
  • Removes all global listeners for enterFrame, exitFrame, frameconstructed, activate, deactivate
  • Removes all stage listeners that have been created by the child.
  • Closes all NetConnection/NetStream and Video.attachNetStream/attachCamera(0) and Microphone.setLoopback(0)
  • Removes AS3 fonts from the global font table
  • Stops sockets, xmlsockets, filereference downloads, other downloading objects (grandchildren SWF), etc.
  • Frees bitmap related to cacheAsBitmap/filter, etc.

After converting my files into Flash CS4 and calling unlaodAndStop() my application worked fine 🙂 .

Thank you various sources..

Keep reading coming soon with some thing amazing about the CPU Memory usage in a multiplayer virtual world 🙂

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,