A load of garbage collection

I recently had the pleasure of debugging an AS3 image viewer application that was crashing after an hour. It seemed to be some kind of memory leak, so I looked for objects not being cleared up by the Flash Player’s garbage collector. I found that, even with AS3’s mark and sweep garbage collection, BitmapData objects left stranded (with no external references) still hog RAM, even when their DisplayObject is deleted.

A similar problem existed in AS2, but will be more acute in AS3 I think, because removing a child DisplayObject from a display list does not delete it (as removeMovieClip() would in AS1/2). Try the following example with your Windows Task Manager or Mac Activity Monitor running, you should see what I mean:


Once the cleanup function runs, all of main’s sprites are removed from the display list, with no more references to them – so the garbage collector should clean things up. But it won’t dispose of the BitmapData objects automatically, so you eventually run out of RAM! This problem of memory hogging occurs even if you keep references to all the clips (in an array for example) and set them to null after removing them from the display hierarchy.

I can see a lot of problems occurring as developers move to AS3 without a firm understanding of how garbage collection works. One answer would be to manually dispose of BitmapData objects or handle the REMOVED_FROM_STAGE event and perform a cleanup as necessary. Here’s quick and dirty fix that cures the memory leak:

InterServer Web Hosting and VPS

Back to school

As you can see, I’ve started a blog. I’ve also officially made the leap to ActionScript 3.0 for all my production. Some things I have noticed:

  • Your memory management needs to be fastidious to avoid memory leaks and weird behaviour (more on this later)
  • It feels more like a real programming language at last
  • It runs A LOT faster. Tell a client this, if they’ve got you maintaining some AS2 dead horse

I’ll be posting stuff I learn about AS3 that you should know as I go… so stay tuned.

Wave your hands in the air

The London Flash Platform Users Group is running a meetup tonight about (among other things) connecting and controlling Flash with the Nintendo Wii controller, using WiiFlash. For those that can’t make it down to the Cosmo Bar in Barbican, they are apparently broadcasting the event here.


…see you there!

Vodafone ‘Mayfly’ website

How do you make the most of now?

I created this microsite for Dare Digital back in 2005, to promote a competition running alongside BBH‘s Vodafone ‘Mayfly’ TV campaign. Users can create a mayfly with a personal message attached, which lives for just 24 hours and wanders around the environment. The best life-affirming message won a drive around Monza with Michael Schumacher in his Ferrari!

Continue reading Vodafone ‘Mayfly’ website