Adobe
Products
Creative Suite
Photoshop Family
Acrobat Family
Flash Platform
Digital Marketing Suite
Digital Publishing Suite
More products
Solutions
Digital marketing solutions
Digital media solutions
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Store
Adobe Store for home and home office
Education Store for students, educators, and staff
Business Store for small and medium businesses
Other ways to buy
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections   Search  
Solutions Company
Help Learning
Sign in Welcome, My orders My Adobe
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Reducing CPU usage in Adobe AIR

by Jonnie Hallman

Jonnie Hallman
  • destroytoday.com

Modified

5 October 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR desktop Flex framerate performance

Requirements

User level

Advanced

Let's be honest. AIR gets a bad rap for being a bloated runtime, using up a lot of precious memory and CPU. Although a lot of AIR applications seem to fall into this trap, it doesn't have to be this way. There are a number of techniques you can use to develop a lightweight application that rivals native programs in terms of performance.

One simple and easy way to drastically reduce CPU usage is through framerate throttling. In this article, I will explain what framerate throttling is and how best to implement it in your application.

Note: To make the most of this article, you should have general knowledge of ActionScript and AIR application development.

What is framerate throttling?

Framerate throttling is the technique of controlling an application's framerate to increase performance when in use and reduce resource usage when idle. As of ActionScript 3, developers have an extremely useful property in their possession—Stage.frameRate. This gem lets you change the framerate on the fly. In previous versions of ActionScript, we were stuck with what we set it to in the IDE. Thankfully, times have changed and there's no longer an excuse for processor-heavy applications lingering in the background.

How do you implement framerate throttling?

Since framerate throttling is essentially a matter of setting the Stage.frameRate property to a lower or higher value, it's up to the developer to decide how involved or advanced it will be. It also depends on the application itself—some allow for more integration than others.

Note: The performance results in the following examples are done on a Macbook Pro 2.8 GHz Intel Core 2 Duo. Since CPU usage is in terms of percent, results will vary from computer to computer.

Novice

The rawest form of throttling is by using the NativeApplication Event.ACTIVATE and Event.DEACTIVATE events—increase the framerate when active, decrease it when inactive. With a single blank window, this results in 1.8% CPU usage when active and .4% when inactive. You can actually set the framerate to .01 on deactivate for .2% usage, but in testing I discovered the window chrome never loses focus.

package { import flash.desktop.NativeApplication; import flash.display.Sprite; import flash.events.Event; public class Application extends Sprite { public function Application () { __init (); } private function __init ():void { NativeApplication.nativeApplication.addEventListener (Event.ACTIVATE, __activate__); NativeApplication.nativeApplication.addEventListener (Event.DEACTIVATE, __deactivate__); } private function __activate__ ($event:Event):void { stage.frameRate = 50; } private function __deactivate__ ($event:Event):void { stage.frameRate = 1; } } }

Intermediate

Certain applications allow more advanced framerate throttling—for example, an application that still needs a level of interaction, even when in the background. Let's say your application has scrollable content to reference and since AIR allows mouse wheel scrolling while in a different application, you need a higher framerate at that time.

In this example, if the application is in the background, but the mouse wheel is scrolling, the MouseEvent.MOUSE_WHEEL handler increases the framerate and sets up an Event.ENTER_FRAME event that will reduce the framerate half a second after scrolling. In cases like these, it's best to have a buffer in place, so you won't change the framerate with every scroll, but also because there's no event for when the mouse wheel is idle.

package { import flash.desktop.NativeApplication; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.utils.getTimer; public class Application extends Sprite { public static const ACTIVE:int = 50; public static const INACTIVE:int = 1; public var active:Boolean; public var scrolling:Boolean; public var buffer:int; public function Application () { __init (); } private function __init ():void { NativeApplication.nativeApplication.addEventListener (Event.ACTIVATE, __activate__); NativeApplication.nativeApplication.addEventListener (Event.DEACTIVATE, __deactivate__); stage.addEventListener (MouseEvent.MOUSE_WHEEL, __mouseWheel__); } private function __activate__ ($event:Event):void { active = true; stage.frameRate = ACTIVE; } private function __deactivate__ ($event:Event):void { active = false; stage.frameRate = INACTIVE; } private function __mouseWheel__ ($event:MouseEvent):void { if (!active) { if (!scrolling) { stage.addEventListener (Event.ENTER_FRAME, __enterframe__); } stage.frameRate = ACTIVE; scrolling = true; buffer = getTimer () + 500; } } private function __enterframe__ ($event:Event):void { if (buffer < getTimer ()) { stage.frameRate = INACTIVE; scrolling = false; stage.removeEventListener (Event.ENTER_FRAME, __enterframe__); } } } }

Expert

If performance optimization is what you live for, you can impress your friends with some intricate framerate throttling. (Note: This won't impress girlfriends.)

In my applications, I like to have transitions from one state to the next for both a smoother environment and a better feel. Because of this, I like to use a high framerate (50). Unfortunately, the higher the framerate, the higher the CPU usage. Therefore, I set the framerate to 50 only when a tween is active. When one isn't, I reduce the framerate to 24. On top of that, there are instances when a loader is animating while the application is in the background. A loader doesn't need 50 fps, so I'll set the framerate to 5 when the application is visible in the background and 1 when not visible.

Note: For this example, I'm using an animate() method to call at the beginning of each tween. Ideally, you would want to build the framerate throttler into your tweening engine, so you wouldn't need to call animate() manually.

package { import flash.desktop.NativeApplication; import flash.display.Sprite; import flash.events.Event; import flash.utils.getTimer; public class Application extends Sprite { public static const ANIMATING:int = 50; public static const ACTIVE:int = 24; public static const INACTIVE_VISIBLE:int = 5; public static const INACTIVE_INVISIBLE:int = 1; public var active:Boolean; public var animating:Boolean; public var buffer:int; public function Application () { __init (); } private function __init ():void { NativeApplication.nativeApplication.addEventListener (Event.ACTIVATE, __activate__); NativeApplication.nativeApplication.addEventListener (Event.DEACTIVATE, __deactivate__); } public function activate ():void { if (!animating) { stage.frameRate = ACTIVE; } } public function deactivate ():void { if (!animating) { stage.frameRate = (stage.nativeWindow.visible) ? INACTIVE_VISIBLE : INACTIVE_INVISIBLE; } } public function animate ($duration:int = 1000):void { stage.frameRate = 50; buffer = getTimer () + $duration; animating = true; if (!animating) { stage.addEventListener (Event.ENTER_FRAME, __checkBuffer__); } } private function __activate__ ($event:Event):void { active = true; activate (); } private function __deactivate__ ($event:Event):void { active = false; deactivate (); } private function __checkBuffer__ ($event:Event):void { if (buffer < getTimer ()) { stage.removeEventListener (Event.ENTER_FRAME, __checkBuffer__); animating = false; if (active) { activate (); } else { deactivate (); } } } } }

Framerate throttling is a small chapter in the optimization of your AIR application's performance. It's a basic way to get your foot in the door and in the mindset of keeping resource usage low. This mentality and practice can easily lead to more responsive applications that enhance the user's experience while leaving a light footprint. Let's face it—no one likes bloatware.

More Like This

  • Creating a socket server in Adobe AIR 2
  • User experience considerations with SQLite operations
  • Exploring the new file capabilities in Adobe AIR 2
  • Building multilingual Flex applications on Adobe AIR
  • Building an XML viewer on Adobe AIR with Flex
  • Creating accessible Adobe AIR applications with the Flex SDK
  • Customer story: AIR - NASDAQ Market Replay
  • Using the Microphone capabilities in Adobe AIR 2
  • Retrieving a list of network interfaces in Adobe AIR 2
  • Resolving DNS records in Adobe AIR 2

Tutorials & Samples

Tutorials

  • Retrieving a list of network interfaces in Adobe AIR 2
  • Writing multiscreen AIR apps
  • Using the AIR 2 NativeProcess API to create a screen recorder
  • Resolving DNS records in Adobe AIR 2

Samples

  • Using the Salesbuilder Adobe AIR sample application

Adobe AIR Forums

More
02/08/2012 CS5.5 - AIR 3.1 - iPhone debugging
02/07/2012 FacebookMobile - logout user?
02/07/2012 Do app updates delete LSOs?
02/02/2012 Facebook iOS?

Adobe AIR Blog

More
02/02/2012 AIRKinect Extension is a Native Extension for use with Adobe AIR...
02/01/2012 Microsoft Kinect and Adobe AIR
02/01/2012 New Adobe Flash Player 11.2 beta for Desktops and Adobe AIR 3.2 beta
01/30/2012 Using charts inside Mobile Applications with Adobe AIR

Products

  • Creative Suite
  • Photoshop Family
  • Acrobat Family
  • Flash Platform
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Mobile apps

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • Adobe Store
  • For students and educators
  • For small and medium businesses
  • For enterprises
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Pacific - English
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).

Ad Choices

Reviewed by TRUSTe: site privacy statement