Skip to main content
Inspiring
January 21, 2011
Question

garbage collection and vectors

  • January 21, 2011
  • 1 reply
  • 1151 views

If I dump a lot of sprites into a vector, then want to garbage collect and repopulate the vector, what is the best way to do this? My app will be reusing the same vector over and over and I'm a bit worried it may have performance issues. Below is some pseudo code that explains.

//create vector

public var sprites:Vector.<Sprite>;

     function someFunction(data:Object):void {

     //check if there are already sprites, if so, delete them

     if(sprites) {

          deleteSprites()

     }

     sprites = new Vector.<Sprite>();

     for(var i:uint = 0; i < data.length; i++) {

          sprites = new SpriteWithPNGsInside();

          addChild(sprite);

     }

}

function deleteSprites():void {

     var spritesLength:uint sprites.length;

     for(var i:uint = 0; i < spritesLength; i++) {

          delete sprites;

          trace(sprites); // traces: Object [SpriteWithPNGsInside]

     }

}

Should I be calling something other than 'delete sprites' like 'sprites.splice(0)'?

I feel like my call to 'sprites = new Vector.<Sprite>()' should wipe them all out but I'm not sure. The sprites actually contain a few png's, and there are quite a few calls to repopulate the vector, so I think eventually there is going to be a major performance problem. Any ideas?

Thanks for the help!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 21, 2011

that delete statement does nothing.

you need to remove each sprite from the displaylist, remove all listeners applied to your sprites (if there are any) and then you need to null all references to your sprites (including its vector element).

Inspiring
January 21, 2011

Thanks again kglad, I really appreciate your work... you've helped me out a lot when I've been in a bind.

I get how to remove them from the stage (removeChild()), I also get how to get rid of the event listeners (removeEventListener() and useWeakReference param), but the thing I don't understand is how to delete the values out of the vector?

Should I iterate through the Vector and pop() the elements off? Or should I shift() them off? I'm going to try to shift() them like this:

public function garbageCollect():void {

     //destroy

     var slength:uint = sprites.length;

     for(var i:uint = 0; i < slength; i++) {

          //remove from stage. this should be the only reference to outside of the vector

          removeChild(sprites);

          //set to null, just to be safe?

          sprites = null;

     }

     //now shift all the elements off and hopefully mark them for garbage collection

     for(i = 0; i < slength; i++) {

          sprites.shift();

     }

}

Then later when i get a new group of sprites I hope to reuse the vector:

sprites = new Vector.<Sprites>();

Then iterate through a new list of Sprites and dump them into this vector. Planning on doing this over and over and over. Any thoughts?

January 21, 2011

You can truncate a Vector the same way you would truncate an Array, using the length property.

sprites.length=0;

public function garbageCollect():void {

     //destroy

     var slength:uint = sprites.length;

     for(var i:uint = 0; i < slength; i++) {

          //remove from stage. this should be the only reference to outside of the vector

          removeChild(sprites);

          //set to null, just to be safe?

                    // not really - you're going to truncate the vector

          sprites = null;     

     }

     // clear the vector

     sprites.length=0;

}