Jump to content
Search Community

How can I remove external swfs completely from memory

jc74 test
Moderator Tag

Recommended Posts

Hi all,

 

I have developed a touchscreen application that works perfectly fine except for the memory issue. Basically, the touchscreen app works kinda like a website with navgation on the side and content displaying next to it. I separeate each section into external swfs, and load each one up when it is needed, thinking it would reduce memory usage on the pc. But unlike actionscript 2, AS 3 would not remove the external swfs from the memory. You can remove it from the stage and you won't see it, but the swf will still stay in the memory! So... as you navigate through the app, loading up all these external swf, even I do loader.unloadAndStop();, the swf is still there, and they just keep stacking up in the memory as you navigate through the app. After leaving the touchscreen for a day or 2, having all these users naviage through it, all the external swfs will just completely consume all the resources and freeze the pc. I have been googling for a day, and still can't find a solution. I hope someone here could point me into a direction to have this issue resolved, and it will be greatly appreciated.

 

Would using loadermax solve the problem?

 

Also, is it better to develop touchscreen application with actionscript 2 since it doesn't have this pressing memory issue?

 

Any help appreciated, thanks!

Link to comment
Share on other sites

Did you remove all event listeners?

 

I believe I did.

 

here is the code that I wrote....

 

By the way, how can I use loaderMax to achieve the same thing?

 


var prevLevel:* = this.parent; 
var currentPage:MovieClip; 
var oldPage:MovieClip; 
var bgID:String; 

function startLoad(txt:String):void {     
var page:String=txt;     
bgID=page;     
var url:String="flash/"+page+".swf";     
var loader:Loader=new Loader();     
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted);     
loadPage(url); 


   function loadPage(txt:String):void {

       var pgeName:String=txt;
       loader.load(new URLRequest(pgeName));
       trace(pgeName);

   }


   function loadCompleted(e:Event):void {

       loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleted);

       oldPage=currentPage;
       currentPage=loader.content as MovieClip;
       loader.unloadAndStop();

       if (oldPage) {
           removeChild(oldPage);

           oldPage=null;
       }
       if (currentPage) {

           while (this.numChildren) {
               this.removeChildAt(0);
           }

           addChild(currentPage);

           prevLevel.slideOffStage();

       }



   }

}

Link to comment
Share on other sites

Would using loadermax solve the problem?

There's a very good chance, yes, because there are several garbage collection issues in Flash Loaders that LoaderMax solves. However, it cannot work miracles - if you add stage listeners in your sub-swfs or start NetStreams or things like that, you must clean those up before unloading the content. That can be done by building your own dispose() method in your sub-swf that listens for the parent's "unload" Event as outlined at viewtopic.php?f=6&t=4212&p=16730#p16730

 

Also, is it better to develop touchscreen application with actionscript 2 since it doesn't have this pressing memory issue?

I certainly don't think so. I'd strongly recommend steering clear of AS2 and shifting to AS3 if at all possible because it's a much more robust language and is far faster. You'll get better performance and your app will be able to last longer because AS2 has been a dying language for a long time. Memory issues aren't inherent to AS3 necessarily - you just need to understand how gc works and how to avoid a few common mistakes. LoaderMax automatically shields you from some of the hassles too. But don't assume that AS2 is superior to AS3 because AS3 is riddled with memory issues - that just isn't true.

 

By the way, how can I use loaderMax to achieve the same thing?

There are a bunch of examples online plus in the ASDocs and there are videos at http://www.greensock.com/loadermax-tips/

 

I noticed you're using nested functions - I'd strongly discourage that because it's frowned upon in ActionScript. It can cause gc problems.

 

Your code would probably look something like this (untested):

import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import flash.display.*;

var prevLevel:* = this.parent;
var currentPage:MovieClip;
var oldPage:MovieClip;
var bgID:String;
var curLoader:SWFLoader;
var oldLoader:SWFLoader;

function startLoad(txt:String):void {
bgID = txt;
loadPage("flash/" + bgID + ".swf");
}

function loadPage(txt:String):void {
var loader:SWFLoader = new SWFLoader(txt, {onComplete:loadCompleted});
loader.load();
trace(txt);
}


function loadCompleted(e:LoaderEvent):void {
oldPage = currentPage;
currentPage = e.target.rawContent as MovieClip;

oldLoader = curLoader;
curLoader = e.target;

if (oldLoader) {
	oldLoader.dispose(true);
}
if (currentPage) {
	while (this.numChildren) {
		this.removeChildAt(0);
	}
	addChild(currentPage);
	prevLevel.slideOffStage();
}
}

Link to comment
Share on other sites

loading works great, but the memory issues didn't go away. Memory just keeps on climbing as I navigate through the app. Even I set the oldLoader and oldPage to null, the previous swfs will just stay in the memory.

 

 

Here is my modified code, it is basically the same except I added:

oldLoader = null;

removeChild(oldPage);

oldPage = null;

 

import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import flash.display.*;

var prevLevel:* = this.parent;
var currentPage:MovieClip;
var oldPage:MovieClip;
var bgID:String;
var curLoader:SWFLoader;
var oldLoader:SWFLoader;

function startLoad(txt:String):void {
  bgID = txt;
  loadPage("flash/" + bgID + ".swf");
}

function loadPage(txt:String):void {
  var loader:SWFLoader = new SWFLoader(txt, {onComplete:loadCompleted});
  loader.load();
  trace(txt);
}


function loadCompleted(e:LoaderEvent):void {

oldPage = currentPage;
  currentPage = e.target.rawContent as MovieClip;

  oldLoader = curLoader;
 curLoader = e.target as SWFLoader;

  if (oldLoader) {	   
   oldLoader.dispose(true);
oldLoader = null;
 removeChild(oldPage);
 oldPage = null;

  }
  if (currentPage) {
     while (this.numChildren) {
        this.removeChildAt(0);
     }

     addChild(currentPage);
     prevLevel.slideOffStage();
  }
}


 

And this is the code I have on the external swf. There is no stage Listener or whatsoever, tweenMax is the only code I used.

stop();

import com.greensock.*;
import com.greensock.easing.*;

var delayTimer:Number = 0.25; 

TweenMax.to(rendering, 1, {x:-805.45, delay:delayTimer, ease:Expo.easeOut});
TweenMax.to(clear_mc, 1, {x:106.9, delay:delayTimer, ease:Expo.easeOut});
TweenMax.to(header, 1.25, {x:-381, delay:delayTimer + 0.25, ease:Expo.easeOut});
TweenMax.to(txt, 1, {x:288.8, delay:delayTimer + 0.35, ease:Expo.easeOut});

 

I am really hitting the wall here.... I have absolutely no idea on how to clear out those external swfs from the memory. Any idea why it isn't working? Bugs in the flash player? That said, I am still really grateful for all the help your provided here, greensock. Thanks!

Link to comment
Share on other sites

I'm having a very hard time replicating this issue. I tried copying your code with a local set of files and it seemed to work fine - gc kicked in eventually and memory was freed up. Can you please post a set of FLA files that I can publish on my end to see the issue replicated?

Link to comment
Share on other sites

I'm having a very hard time replicating this issue. I tried copying your code with a local set of files and it seemed to work fine - gc kicked in eventually and memory was freed up. Can you please post a set of FLA files that I can publish on my end to see the issue replicated?

 

I sent you a link to download the flas. Please check your pm. Thanks a lot!! Much appreciated!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...