Adobe
製品
Creative Suiteファミリー
Photoshopファミリー
Acrobatファミリー
Flash Platform
Digital Marketing Suite
Digital Enterprise Platform
Digital Publishing Suite
その他の製品一覧
ソリューション
コンテンツオーサリング
教育
金融機関
デジタルマーケティングソリューション
その他のソリューション
ラーニング サポート ダウンロード 会社情報
ご購入
アドビストア安心のサポート& サービス
アカデミック版のご購入学生、教職員、個人
ライセンスのご購入企業、教育機関、官公庁
販売パートナー
検索
 
情報 サインイン
ようこそ、 カート 注文状況 ユーザー登録
マイアカウント
サインアウト
サインインの目的 お客様のアカウントや体験版ダウンロード、製品の拡張機能、コミュニティエリアへのアクセスなどを管理するため
Adobe
製品 セクション   検索  
ソリューション 会社情報
サポート ラーニング
サインイン ようこそ、 注文状況 ユーザー登録
Qty:
Subtotal
Checkout
Adobe Developer Connection / Adobe AIRデベロッパーセンター /

Reducing CPU usage in Adobe AIR

著者 Jonnie Hallman

Jonnie Hallman
  • destroytoday.com

Modified

5 October 2009

ページ ツール

Facebookでシェア
Twitterでツイート
LinkedInでシェア
ブックマーク
印刷

Tags

必要条件

ユーザーレベル

上級

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

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

製品

  • Creative Suiteファミリー
  • Photoshopファミリー
  • Acrobatファミリー
  • Flashプラットフォーム
  • Digital Marketing Suite
  • Digital Enterprise Suite
  • Digital Publishing Suite
  • モバイルアプリ

ソリューション

  • カスタマーエクスペリエンスマネジメント
  • コンテンツオーサリング
  • デジタルマーケティング

業種別ソリューション

  • 教育
  • 金融機関

サポート

  • ヘルプ&サポート
  • 注文と返品
  • ダウンロードに関するヘルプ
  • ユーザー登録に関するヘルプ

ラーニング

  • ADC: Adobe Developer Center
  • Adobe TV
  • Design Magazine
  • Photoshop Magazine
  • Focus In

ご購入方法

  • アドビストア
  • アカデミック版のご購入
  • ライセンスのご購入

ダウンロード

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

会社情報

  • プレスルーム
  • パートナープログラム
  • 企業の社会的責任(英語)
  • 採用情報
  • 投資家の皆様へ(英語)
  • イベント&セミナー
  • Legal(英語)
  • お問い合わせ
国・地域および言語の選択 日本(変更)
国・地域および言語の選択 閉じる

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
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • España
  • France
  • Deutschland
  • Hrvatska
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Magyarország
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Österreich - Deutsch
  • Polska
  • Portugal
  • România
  • Россия
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Slovenija
  • Slovensko
  • Srbija
  • Suomi
  • Sverige
  • 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.

当Webサイトをご利用のお客様は、利用条件およびプライバシーポリシー(2011年9月30日更新)にご同意いただいたものとみなされます。

Reviewed by TRUSTe: site privacy statement