If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
removeChild() does not always remove the object from memory. removeChild() and removeChildAt() actually only remove the object from their parents DisplayObjectContainer. If the removed object is referenced by a variable or some such other thing the object will still persist in memory until the variables are cleared or set to null.
for example we draw a circle and attach it using the following
var cir:Shape = new Shape(); cir.graphics.lineStyle(1); cir.graphics.beginFill(0xFF0000); cir.graphics.drawCircle(100,100,50); parent.addChild(cir);
now if we use the removeChild() to delete the shape
parent.removeChild(cir);
It is now longer viewable in the movie, but because it is referenced by the variable cir, the object will actually still persist in memory.
so you can re-add the shape by using
parent.addChild(cir);
If you wanted to totally remove the shape from memory you would need to change or assign a null to every var that references the shape, in our case you would do this
parent.removeChild(cir) cir = null;
Even then it doesn’t remove the shape from memory it is now just eligible for garbage collection. (more about grabage collection another day). But for all intsive purposes it is gone.
Found this useful? How about buying me a coffee - simply click here.


Ok I figured you needed to set the class you created to null or something to actually make Flash forget about it. I’m building an avatar builder and reload the body parts when they change from eyes to say hair. I decided to create instances of linkage setup in the library to create the selection list. Before loading new assets in I used removeChildAt() inside a for statement but felt in my gut that each time I built the menu and removed the previous class references for the list they were still hanging out so it would grow and grow and grow every time you changed what body part you were working one and reloaded the selection.
Since these will be in stores and touch screens people might be using them all day one after another by the end of the day and maybe if employees don’t turn off the computers the amount of memory taken up by old display objects / movieclips / class instances / whatever they are called this year would be hanging around by the hundreds of thousands.
Anyway I’ll set the class instance / reference to the class instance = null. Since I create them inside a function I wonder if I’l have access to the instance names by reference when I rerun the function later. The are not private or anything and are not in an external as file / package / class whatever you call includes now.
Hi jeff. What you have done is correct. The errors that you were getting were to the new garbage collector as mentioned. But Yes Like you have done you also need to clear all the classes. Also to note is EventListiners as they may cause a class never to be cleared just as you were finding. Check here for more info.
http://www.flashcs.org/memory-managment-memory-leak/
Also I have created a memory guauge which helps you to see if you have leak somewhere in your app
Click here to get it: http://www.flashden.net/item/flash-memory-gauge-performance-check/17981
Have fun