Optimizing Flash Performance
With some recent experience related to application performance, I thought to share some strategies to optimize performance of applications made with Flash Professional. There are two main components that determine Flash performance:
- CPU/GPU usage
- Memory usage
In this article I will begin with discussing memory management guidelines. Several times we are using filters, loops, Timers SoundChannels etc in Flash applications which are playing an important role in application performance.
Easiest techniques for performance optimization:
- Some objects or movie clips required just to represent some data, which doesn’t require user interaction in such cases normally we are not writing any code for that object. But still when user click on such object Flash Player checks for mouse interactions when these objects are present. You can save some CPU cycles by disabling mouse interactivity for objects that do not require mouse interactivity. Disable mouse interactivity.
- The issue with Timers is more critical. If a Timer has not stopped (because its currentCount is less than its repeatCount or because a stop() method has not been applied to it), the Timer will not respond to garbage collection even if you remove its listener and null all references. A Timer’s listener function won’t be called again once you remove the listener, but the Timer still consumes memory. Several times we start timer to achieve function requirement, and even after completion of the functional part execution the timer running. Explicitly stop Timers for garbage collection. However, if you open, play, and then close a Flash game running on a mobile device repeatedly without ever restarting the game, you may see a noticeable problem.
- For ENTER_FRAME event loop, (if required to use then only) use different listeners and different listener functions applied with related DisplayObjects only.
- Avoid use filters. Keep in mind that whenever you apply a filter to a DisplayObject in Flash, its cacheAsBitmap property gets forced to true and Flash must use memory to store that bitmap. If anything changes about that DisplayObject, like its transform operations like size or rotation or a child updates visually, etc, it must regenerate the bitmap, thus using more memory. That old bitmap hangs around until gc kicks in.
- Always use reverse for-loops. Avoid writing do-loops and while-loops.
- Using correct type of display objects for each element
- Use weak event listeners and remove listeners when no longer needed.
- Strictly type variables whenever possible.
- Explicitly disable mouse interactivity when mouse interactivity is not required.
- Replace dispatchEvents with callback functions whenever possible.
- Stop Sounds to enable garbage collection for Sounds and SoundChannels.
- Use the most basic DisplayObject needed for each element.
- Always use cacheAsBitmap and cacheAsBitmapMatrix with air apps (mobile devices).
- Several times when we are playing with loop and require to create object for each iteration in that case. In such situation it would be great if we create one object outside the loop and reuse it repeatedly in loop.
Friends, I hope these would be helpful to optimization of application performance. Feel free to share more ideas and comments about the topic.
Enjoy RIA… 🙂