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:

  1. CPU/GPU usage
  2. 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… 🙂

Advertisements
  1. Earl
    February 9, 2013 at 9:03 PM

    Good Article… just wanted to add:

    1) Always convert your vector clips to bitmaps whether before hand or via script. Vectors on the stage take time to render compared to bitmaps. Animated bitmaps will perform better than animated vectors.

    2) Assign object properties to a variable if you plan to reference it often.
    e.g. var x:int = stage.width / 2 + stage.width;
    should be
    var w:int = stage.width;
    var x:int = w / 2 = w;

    3) Recycle objects. It’s takes less effort compared to creating new objects every time. Set up a class that creates and stores objects, and just call the class’ methods if you want to create or remove objects (from the display). Objects removed from the display are stored by the recycleBin until you request for the object again.

    4) if an object is off-stage, set its visible property to false. If it’s animating, stop it.

    5) Never use scrollRect on an animated displayObject, or on a displayObject with animating children.

    6) Use Vectors instead of Arrays (if possible)

    7) var x:int = (a < b) ? 1 : 2;
    is faster than:
    var x:int;
    if (a < b) x = 1;
    else x = 2;

    8) Don't use constants… use variables instead.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: