views:

36

answers:

2

Hi! I'm trying to remove simple objects from memory, but when I call removeChildren memory usage rose :/ And I don't why ? And how can I remove objects ?

package {
    import flash.display.DisplayObject;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.system.System;

    public class Main extends Sprite {

        public function Main() 
        {
            for (var i:int = 0 ; i < 1000;i++) {
                var addBouncerButton:SimpleButton = new SimpleButton();
                addBouncerButton.x = 100;
                addBouncerButton.y = 10;
                addBouncerButton.name = "Btn"+i;
                addChild(addBouncerButton);
            }
            stage.addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, remove);
        }

        private function remove(e:KeyboardEvent):void 
        {
            trace("Children : " + this.numChildren);
            trace(System.totalMemory * 1024 + " kb");
            if(this.numChildren > 0)
                var o:DisplayObject =  removeChildAt(this.numChildren - 1);
            o = null;
        }

        private function update(event:Event):void
        {
        }
    }
}
+2  A: 

You can use the delete keyword to queue your object for garbage collection. This garbage collector could take a while (few ms) to do it's job though, and there should be no trailing references to your objects.

For a better understanding of as3 GC you can read this excellent article: http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html

Good luck!

Johan
@Johan. I'm afraid you're wrong about 'delete'. 'delete' is used to remove dynamically created properties, such as entries (and keys) from Object objects and dictionaries.
Juan Pablo Califano
Of course @Juan is right, I mixed up as2 and as3. You should make sure to remove all references to the object and the as3gc will pick them up. Read this article for a better understanding of the as2/3 delete differences http://www.gskinner.com/blog/archives/2006/06/understanding_t.html
Johan
A: 

Remove all references to an object so for example if you have eventlisteners remove those and set the object to null. The garbage collector will then clean that object for you some time you dont need to worry about it.

slayerIQ