By Jun (John) Chen
30 November 2011


Prerequisite knowledge

This article assumes you have previous experience working with Flash Professional. Prior experience programming with ActionScript 3 is also recommended.

User level


Required products

Sample files

Have you ever wondered why console games with lower available CPU cycles, like the earlier versions of Sony PlayStation (PS1 ~33 MHz, PS2 ~300 MHz) perform much better than some Flash games on desktop or mobile devices with 600 MHz to over 1 GHz? It's not a fair comparison to evaluate the platforms based on CPU cycles alone because there are significant differences in both hardware and software when delivering games on different platforms. To get the optimal performance with any computer or device, it is important to maximize the hardware capabilities as well as implementing software development best practices. In this article, I'll describe development practices that help improve performance of mobile AIR apps.

Improving performance by updating the AIR SDK

The fastest way to optimize performance and fix bugs that may be slowing down mobile apps involves updating your AIR SDK. Unfortunately, there isn't an .exe or .app installer that will facilitate this for you. However, you can follow the two-step process outlined below to update your AIR SDK quickly and easily:
Step 1
Download and unzip the newest AIR SDK into the application folder FlashProCS5.5/AIR2.6/, replacing the contents. Don't worry about renaming any of the folders.
Step 2
Copy the file ../frameworks/libs/air/airglobal.swc from the unzipped AIR SDK and replace the older one in your FlashProCS5.5 install folder ../Common/Configuration/ActionScript 3.0/AIR2.6/
Going forward, just ignore the fact that items will be labeled as AIR2.6. Although you've not updated the folder name or paths, you can be confident that your SDK has been updated to use the newer version. Although you could change the string to match the SDK version number by updating an XML file somewhere in Flash Professional, it is not necessary.

Applying manual garbage collection

The term manual garbage collection doesn't refer to triggering the automatic garbage collection that it runs in the background in Flash Professional from time to time. Instead, I'm suggesting that when you program mobile apps, you have to take care to clean up your own mess. Every class you create should include a cleanUp function that removes its own listeners, timers, loaders, and unused movie clips when they are no longer needed. To keep things simple, here are two different ways to apply garbage collection to your code:
Method 1
If your class is able to detect when its usefulness ends, then call its custom cleanUp() function. For example, let's say your game has many ball instances flying across the screen from right to left. You know that once the ball is no longer visible on the screen (x < 0), the ball is no longer useful. If that is the case, you can call cleanUp() whenever that happens.
Method 2
Sometimes it is more efficient to create a single timer that will detect whether or not individual instances in a batch of objects are still useful. This approach can save CPU cycles by using a much bigger delay in your timer. For example, you can set up a function that checks for usefulness of objects every two seconds instead of every 100 milliseconds.
Here's an example of a cleanUp function:
public function cleanUp(){ removeEventListener(MouseEvent.ROLL_OVER, meRollOver); removeEventListener(MouseEvent.ROLL_OUT, meRollOut); playTimer.stop(); playTimer.removeEventListener("timer", playHandler); }
And here's an example of a cleanUp timer function:
private function cleanUpTimerHandler(e:Event){ var tempNumChildren:int = playContainer.numChildren; var tempMovie:MovieClip; for(var i=0; i < tempNumChildren; i++){ try{ tempMovie = playContainer.getChildAt(i) as MovieClip; if(tempMovie.scaleX < .1){ tempMovie.cleanUp(); playContainer.removeChildAt(i); } }catch(e:Error){break;} } }
The cleanUp function is pretty self-explanatory; so let's take a look at the cleanUpTimerHandler(), which is available in the sample files folder at this location: cleanTimersAndListners/code/
In this sample application, triangle objects are constantly being generated and added to the playContainer movie clip. In the cleanUpTimerHandler function, each movie in the playContainer is analyzed and tested to check whether or not it is still useful. In this case, the terms of usefulness depend on the object's x scale (the triangles continue to decrease in scale over time). Once triangle is so small that it is not visible, the cleanUpTimerHandler method removes the instance from the display list.
To see demos showing the usefulness of garbage collection, watch the following videos:
Manual garbage collection
These animations on the iPod 4 (top) and Nexus One (bottom) perform well, keeping under 200 triangles in playContainer and never slowing down.
No manual garbage collection
These animations on the iPod 4 (top) and Nexus One (bottom) perform poorly, shown by the ever-increasing number of triangles in playContainer as the animation plays.