Caching timeline objects in Flash

Grant Skinner‘s talking about a Flash feature request that seems to make a lot of sense. It got me thinking about a problem I’ve experienced with Flash in the past, and whether it deserved anyone’s attention at Adobe. I decided that it did.

A lot of programmers new to AS3 reach a point, as I did, when they get very jumpy about their projects’ memory usage. It’s kind of a rite of passage, and during mine, I discovered that a set of animated buttons I incorporated into a GUI were spewing out gobs of display list garbage. (Display list garbage consists of DisplayObject instances, whose references to parents and children make them difficult for the garbage collector to quickly eliminate, kind of like digesting cellulose. For more on garbage collection, I’d suggest you conduct a hexosearch.)

Imagine your Flash animator friend makes you a MovieClip that represents a flashing bulb. She has probably made it go on and off by having a blank keyframe (lightbulb off) and a filled keyframe (lightbulb on) in a top layer in the Flash timeline, with the lightbulb outline in a bottom layer. Because the timeline is short, it will loop frequently, causing the bulb to flicker.

Now, let’s consider what happens in the Flash Player with this MovieClip. When the lightbulb goes on, a display object is instantiated and added to the display list. When the bulb goes off, that display object is removed from the display list and discarded. The lightbulb outline in the background stays put. This is the procedure specified by the Flash timeline that the MovieClip follows each time it loops– and it loops very frequently. The result is a small animation that has produced a heaping pile of display list garbage.

One might think of this and say, “Well, why can’t the MovieClip timeline cache these display list objects? It could clearly benefit from reusing them.” Caching does seem like a good idea, but only for short, looping animations. If every animation on Newgrounds cached the display objects created on the timeline, you’d end up with a ton of cached display objects that do not need to be reused, because the animation doesn’t loop. The timeline has no way to determine by itself whether it should cache its animated contents or not.

But as authors, we know when a timeline should cache its contents. My feature request calls for a Boolean property on the MovieClip class (similar to cacheAsBitmap on DisplayObject) that allows developers and animators to explicitly set the behavior of MovieClip timelines for the purpose of conserving memory. If you agree that it matters, why don’t you vote for it.

You can leave a response, or trackback from your own site.

Leave a Reply