Skip to main content
Chipleh
Inspiring
August 13, 2010
Question

Best practices for using .load() and .unload() in regards to memory usage...

  • August 13, 2010
  • 2 replies
  • 551 views

Hi,

I'm struggling to understand this, so I'm hoping someone can explain how to further enhance the functionality of my simple unload function, or maybe just point out some best practices in unloading external content.

The scenario is that I'm loading and unloading external swfs into my movie(many, many times over) In order to load my external content, I am doing the following:

Declare global loader:

var assetLdr:Loader = new Loader();

Load the content using this function:

function loadAsset(evt:String):void{
var assetName:String = evt;
if (assetName != null){
  assetLdr = new Loader();
  var assetURL:String = assetName;
  var assetURLReq:URLRequest = new URLRequest(assetURL);
  assetLdr.load(assetURLReq);
  assetLdr.contentLoaderInfo.addEventListener( Event.INIT , loaded)
  assetLdr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, displayAssetLoaderProgress);
  function loaded(event:Event):void {
   var targetLoader:Loader = Loader(event.target.loader);
   assetWindow.addChild(targetLoader);
  }
}
}

Unload the content using this function:

function unloadAsset(evt:Loader) { 
trace("UNLOADED!");
evt.unload();
}

Do the unload by calling the function via:

unloadAsset(assetLdr)


This all seems to work pretty well, but at the same time I am suspicious that the content is not truly unloaded, and some reminents of my previously loaded content is still consuming memory. Per my load and unload function, can anyone suggest any tips, tricks or pointers on what to add to my unload function to reallocate the consumed memory better than how I'm doing it right now, or how to make this function more efficient at clearing the memory?

Thanks,

~Chipleh

This topic has been closed for replies.

2 replies

Inspiring
August 14, 2010

Since you use a single variable for loader, from GC standpoint the only thing you can add is unloadAndStop().

Besides that, your code has several inefficiencies.

First, you add listeners AFTER you call load() method. Given asynchronous character of loading process, especially on the web, you should always call load() AFTER all the listeners are added, otherwise you subject yourself to unpredictable results and bud that are difficult to find.

Second, nested function are evil. Try to NEVER use nested functions. Nested functions may be easily the cause for memory management problems.

Third, your should strive to name variables in a manner that your code is readable. For whatever reason you name functions parameters evt although a better way to would be to name them to have something that is  descriptive of a parameter.

And, please, when you post the code, indent it so that other people have easier time to go through it.

With that said, your code should look something like that:

function loadAsset(assetName:String):void{
     if (assetName) {
          assetLdr = new Loader();
          assetLdr.contentLoaderInfo.addEventListener(Event.INIT , loaded);
          assetLdr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, displayAssetLoaderProgress);
          // load() method MUST BE CALLED AFTER listeners are added
          assetLdr.load(new URLRequest(assetURL));
     }
}
// function should be outside of other function.
function loaded(e:Event):void {
     var targetLoader:Loader = Loader(event.target.loader);
     assetWindow.addChild(targetLoader);
}

function unloadAsset(loader:Loader) {
     trace("UNLOADED!");
     loader.unload();
     loader.unloadAndStop();
}

El_Arcon
Inspiring
August 14, 2010

well from what i've read on the net it says that if you really want an object/variable to be out of the memory you need to do this:

nameOfObjectorVariable = null;

with that way it will definetly nothing of the related variable / object be left in the memory..,

hope it helps..,