ActionScript 3 Memory Optimization
September 25, 2009
I was recently finishing up a kiosk project wherein a Flash projector would have to run fullscreen 24/7, and was having a terrible time pinpointing a memory leak that left the projector unresponsive after only a few hours. While I was able to improve memory usage, I couldn’t get the app to stay calm over an extended period. The end result was a workaround that had the projector restart itself after an inactive waiting period.
The Problem #
I was working with a proprietary AS3 framework that relied on multiple SWFs and XML for configuration and content. The piece involved lots of lossless images (dynamically loaded) and only needed to run as a projector from a Windows machine. It also communicated with an external series of lights via a MAKE Controller board. Initial testing of the app went smoothly, but letting it run overnight turned it into a sluggish memory hog.
Improving Memory Usage #
There were a few tips I followed from various sources to streamline the app’s memory usage. Ultimately they weren’t enough to solve the big problem, but I watched the numbers drop in Apple’s Activity Monitor.
- Remove unused event listeners.
- Clear unused display objects by setting each obj = null.
- Make sure you’ve deleted any child display objects with removeChildAt()—great in a loop.
- Stop all Timers to free up the memory that they use. myTimer.stop();
Final Solution #
We came to two options: use a third-party product for compiling a more efficient Flash .exe (such as Screenweaver, Zinc, or MProjector), or create a reliable workaround that would simply restart the kiosk app and avoid the memory leak in the first place. The client needed files delivered quickly and did not have extra budget, so we ended up choosing the latter. We avoided the problem by adding a timer that would restart the Flash piece after 10 minutes of inactivity. (This was done by using FSCommand to call a batch file, since we’re using windows, immediately closing the Flash app, and having the batch file execute the Flash app again.) This ended up working pretty well and allowed us to deliver everything on time.
There are more discussions about memory optimization with Flash projectors (and Flash in general), but I never found any silver bullets. Here are a handful of links that were helpful: