Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
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

  • Using the SQLite database access API in Adobe AIR
  • Writing multiscreen AIR apps
  • Creating accessible Adobe AIR applications with the Flex SDK
  • 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
  • Resolving DNS records in Adobe AIR 2
  • Using drag-and-drop support of remote files in Adobe AIR 2
  • Retrieving a list of network interfaces 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
04/11/2012 Surround sound 5.1 with Air 3.2 on desktop app?
12/12/2011 Live Streaming H.264 Video on iOS using AIR
04/17/2012 HTMLLoader - Google Maps?
04/12/2012 Tabindex in forms on mobile?

Adobe AIR Blog

More
07/09/2012 Protected: Publishing Adobe AIR 3.0 for TV on Reference Devices
07/08/2012 Source Code: Adobe AIR 3.3 Retina Video Application
07/06/2012 Application specific File Storage on Adobe AIR based ios Application
07/04/2012 Recent Work - iPad/Android App: Inside My toyota

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

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

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • 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
  • Security
  • 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
  • 台灣

Southeast Asia

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

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement