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.


1 Response to “removeChild and removeChildAt memory fun”
Leave a Reply