Archive for January, 2008

AS3 for Noobs (Part 4): Garbage collection

Friday, January 4th, 2008

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. (more…)