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

RoryRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blue-genieCommented:
i feel your pain, but how was it bettter in AS2 - ADboe GC is still shocking but better (IMHO)

then in your snippet where are you actually removing the items (removeChild)
0
RoryRAuthor Commented:
this["z" + level].removeChild(this[cull]);

also tried many other ways.



maybe, but I am not impressed so far in the AS3 GC
0
RoryRAuthor Commented:
fixed it myself after more searching!

"I always use weak reference for eventListeners (the last param set to true):
addEventListener(eventStr, eventHandlerFunc, false, 0, true);
This makes the Garbage Collector go round."

also changed my cull function to:
      this[cull].parent.removeChild(this[cull]);
      delete(this[cull]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
courtthreeCommented:
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!
0
RoryRAuthor Commented:
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 :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.