Skip to main content
Participating Frequently
October 16, 2015
Answered

Removing loaded external swf doesn't decrease memory

  • October 16, 2015
  • 1 reply
  • 1086 views

I am developing desktop application using flash builder 4.7, where I am loading external swf from main swf, but after removing it, it stays in processes, (checking in task manager. os: windows 7), process decreases a little bit, but not as much as it should be. so every time I load it again and remove, process increases and after multiple load, application crushes down. I am removing it like this

if (_loader.numChildren>0)

     _loader.removeChildren(0,_loader.numChildren-1);

_loader.unload();

_loader.stopAllMovieClips();

_loader.unloadAndStop(true);

_loader.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

this.removeChild(_loader);

_loader = null;

I added custom destroy method inside external swf and started calling it. I was removing nearly everything, (nearly saying because it could be something that I've missed, because it's huge project) but still the process remaind same.

Than I created empty swf (new project with no code and nothing in it) and tried to embed it and see what happens. It was all the same...

Then I thought new Air SDK could have fixed this problem, so I updated SDK to 19.0.0.207 currently latest, but still nothing, even though now the process increases higher.

I think it's Flash's gc issue. One of the solution is that to have one and only instance of external swf and removeChild and addChild it everytime you need it. It is kind of bad way to do so. Processes wont increase all the time, but in my case I have many swfs to load and it will slow down my application and for low RAM resource computers it may crush, and It's bad to have unnecessary thing running in background process

so any suggestions are welcome

This topic has been closed for replies.
Correct answer nezarov

Here is my full test project code:

public class LoaderTest extends Sprite

{

  private var _button:Sprite;

  private var _link:String = "EmptyProj.swf";

  private var _loader:Loader;

  private var _url:URLRequest;

  private var _removeButton:Sprite;

  public function LoaderTest()

  {

  _url = new URLRequest(_link);

 

  _button = new Sprite();

  buttonGen(_button, 0xCCCCCC);

  this.addChild(_button);

  _button.x = 20;

  _button.y = 20;

  _button.addEventListener(MouseEvent.CLICK, onClick);

  _removeButton = new Sprite();

  buttonGen(_removeButton, 0xFF0000);

  _removeButton.x = 20;

  _removeButton.y = 20;

  _removeButton.addEventListener(MouseEvent.CLICK, remove);

  }

 

  protected function remove(event:MouseEvent):void

  {

  if (_loader.numChildren>0)

  _loader.removeChildren(0,_loader.numChildren-1);

  _loader.unload();

  _loader.stopAllMovieClips();

  _loader.unloadAndStop(true);

  _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);

  this.removeChild(_loader);

  _loader = null;

  this.removeChild(_removeButton);

  this.addChild(_button);

  }

 

  protected function onComplete(event:Event):void

  {

  this.addChild(_loader);

  this.addChild(_removeButton);

  this.removeChild(_button);

  }

 

  private function buttonGen(butt:Sprite, color:uint):void

  {

  butt.graphics.beginFill(color);

  butt.graphics.drawRect(0,0,200,80);

  butt.graphics.endFill();

  }

 

  protected function onClick(event:MouseEvent):void

  {

  _loader = new Loader();

  _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

  _loader.load(_url);

  }

}

EmptyProj.swf code here

public class EmptyProj extends Sprite

{

     public function EmptyProj()

     {

    

     }

}


No need to stop the movie clips and to remove them.. use unloadAndStop() and it'll stop everything and unload the file.. as  it's not possible to call the loader contents (the movieClips) to stop after unload method, it's already removed.. So try to use that 3 lines only to unload the file.

1 reply

Inspiring
October 16, 2015

Try:

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);


_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);

_loader = null;

Participating Frequently
October 16, 2015

I've already had _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); added. after load is complete onComplete is called, I addChild those loaded stuff.

when I try to remove tham I call all those code written in my question. In any case, if I didn't addEventListener Event.COMPLETE, it would throw an erron on removeEventListener Event.COMPLETE, so thats not a case

Inspiring
October 16, 2015

You are using loaderInfo

This code working well here:

var _loader:Loader  = new Loader();

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE , onComplete);

function onComplete(e:Event):void {

  

    MovieClip(_loader.content).getChildAt(0).y = 100;

    MovieClip(_loader.content).removeChild(MovieClip(_loader.content).getChildAt(1));

    addChild(_loader);

}

stage.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

    _loader.unload();

    removeChild(_loader);

   

}

_loader.load(new URLRequest("data/external.swf"));