Link to home
Start Free TrialLog in
Avatar of RoryR
RoryR

asked on

AS3 memory leak, how to unload objects?

I am having great problems unloading objects that are dynamically added using a loader. My program loads lots of images with one loader, and after it reaches a maximum amount of images I cull off the oldest. Problem is they stop displaying but stay in Ram.

I am aware from hours of googling that you need to stop all refrences and let the garbage collector delete it (which is shit design by adobe in mho btw). but how ever hard I try it just will not unload!

I have included some snippets taken from all over my code and combined to make sense, I can't really do anything more helpful, too much source code to post and don't want to reveal my source code. but I have put in every bit that adds a child or loads into it.

what I want is to be able to delete these jpgs when needed and have my memory freed. bloody adobe making life harder! what was wrong with how it was done in AS2? *sigh*


var imageloader:Loader = new Loader();

imageloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded_image);

function load_next_image(){
imageloader.load(new URLRequest(currently_loading +  ".jpg"))
}

function loaded_image(e:Event){
loaded_images.push(currently_loading);
	
//this[currently_loading] = new BitmapData(imgsize,imgsize);
this[currently_loading] = imageloader.content;
this["z" + level].addChild(this[currently_loading]);
}

function cull_image() {
var cull:String
cull=loaded_images.shift();

this["z" + level].removeChild(this[cull]);
			  //this[cull].parent.removeChild(this[cull]);
imageloader.unloadAndStop(this[cull]);
}

Open in new window

SOLUTION
Avatar of blue-genie
blue-genie
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RoryR
RoryR

ASKER

this["z" + level].removeChild(this[cull]);

also tried many other ways.



maybe, but I am not impressed so far in the AS3 GC
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can I just ask a quick question about how you're measuring memory usage?

I have had exactly the same issues as you're describing. Desperately hunting round for references to clips/loaders so I can manage memory.

Your point about the weak reference is completely valid of course.

However, my main issue arose out of checking memory requirements in my local Flash player! The GC, when played locally, behaves very differently to when run inside a browser...

I was tracing memory usage in the local FP and spent a weekend panicking about an ever escalating memory requirement. I then uploaded to the server and started hitting a text box on the stage with the online memory usage and found that GC was doing a brilliant job in reality!

I am sure you have arrived at the same conclusion independently but just wanted to raise this point. Bottom line - what can seem like a memory leak in testing can often be totally fine in the browser!
Avatar of RoryR

ASKER

yeah, tried it in browser and used task manager to monitor it, as like you said in the flash ide it does memory leak when it doesn't in browser.

was still getting an issue until I did the weak reference fix.

thanks :)