AS3 for Noobs (Part 4): Garbage collection

So maybe you’ve heard of this whole garbage collection thing and you’re wondering what it is and why should you care. I’m going to give you a simplified description of what it is and the best practices I’ve found to deal with it. Basically in AS2 when you removed an object like a movieclip or something, you no longer had to worry about it, it was gone. It either was there on stage or it didn’t exist. For instance in my gallery application are a lot of photos get loaded at one time and I had to limit how many displayed at a time because the more that were on stage the slower flash would get. So I would display 100 then when I wanted to display another 100 I would just remove the first 100 and everything would work just fine.

Well now since display objects don’t live and die on the stage, you can remove a clip from the stage but it still exists. It’s just waiting “back stage” in case you need to use it again. This is great for some things but like in my gallery example above it causes a real problem. I can remove the first 100 thumbnails from the stage but they are still there hogging up memory. If you remove an object from stage and you don’t use it again for a long time, the flash player will mark it for removal so that it actually will disappear from memory. This process is called “garbage collection”. The problem is that if any other object references it then it won’t ever be marked for removal because flash still thinks it’s needed.

So let’s say you have a bunch of thumbnail movieclips and you keep track of the selected one by referencing it in another variable like this:

var selectedThumb = this;

When you remove the thumbnail it will never get marked for garbage collection because flash sees that selectedThumb still references it and therefore it thinks it’s still needed for something. You might never run into a problem with this, but if you have large applications that load in a lot of images or external swf files, things start to pile up really quickly and it can cause flash to slow down or even lock up all together.

So as a best practice this is what I’ve come up with. For every class I write that I want to live and die on the stage I have created a function that initiates the class and I have a function that destroys it when it’s removed from stage. The function that initiates the class is really nothing new it’s just doing the reverse that I’ve added. It looks something like this:

package   { public class MyObject extends MovieClip { // create variables var instance:MovieClip; var bmp:Bitmap; // function to destroy all references and objects public function destroy(e:Event) { // removes all children attached to this object while (numChildren !== 0) { removeChildAt(0); } // set instance to null because it references this object instance=null; // a bitmap won't be removed unless it is destroyed like this bmp.bitmapData.dispose(); bmp=null; } public function MyObject() { instance=this; // adds a listener that calls the destroy function once this object is removed from the stage addEventListener(Event.REMOVED_FROM_STAGE,destroy,false,0,true); } // add methods and other stuff here } }

As you can see from this class it has a function called “destroy” that basically prepares it to be garbage collected once it’s removed from the stage. I know this might seem like a pain to do but believe me it’s easier to do this then to suddenly find out you have a huge memory leak and have to go back through all your code to find out what’s causing it. I know I didn’t go into too much detail on it so just let me know if you have any specific questions.

One Response to “AS3 for Noobs (Part 4): Garbage collection”

  1. Ryan D Says:

    Great post. Do you have a solid method for preventing the garbage collection of objects in Tween. I’ve switch over to Tweener for now but would like to use the built-in classes that Flash offers. Any suggestions on this?

    Thanks,
    Ryan

Leave a Reply