`

10 Tips for Flex Application Performance

    博客分类:
  • Flex
阅读更多

We're going to keep this post lean and mean, and get down to business with 10 Tips that will keep your Flex applications fast, lean, and responsive.

Rule # 1: Clean up after yourself

In general, it is good practice to maintain clean code. Not only in the sense of having properly formatted and readable code, but also code that leaves nothing behind... no memory leaks, no cpu hogs, nothing but a clean object that can be reclaimed by the GC.

1) Manage your event listeners - this message is two fold. First, you should always remove event listeners that are no longer needed. They can lead to object references that prevent the garbage collector, which equates to memory leaks, which can be very difficult to track down and detrimental to application performance. You can use weakly referenced event listeners to minimize memory leakage, but you still should explicitly clean them up when you don't need them anymore. The second factor is that failure to remove event listeners can cause performance issues. Event handlers could be firing within your application, which you weren't even aware of. You dispatch an event in a child component, and there could be handlers up the DOM tree (parent objects) that are also firing on the same event. If you don't want this to happen, be explicit with your event handlers; make them handle specific event types, and get rid of them when your application doesn't need them anymore.

2) Unload loaders - any time that you are using an object based on a loader (Image, SWFLoader, etc...), it's a good practice to call unloadAndStop() to unload the content from the loader, and invoke the GC. This will free up valuable system resources and cpu cycles won't be wasted if they aren't needed. I typically even do this for static image files, to prevent memory usage from creeping up.

3) Dispose of things - I find it to be a very good practice to create "dispose()" functions in your custom components, data managers, or views that will clean up the object's resources. The dispose() method will need to be explicitly invoked when you are finished using an object, but that dispose method will handle everything needed to clean up an object and free it's resources. For example, stop timers, remove event listeners, unload loader objects, set variable references to null, etc... Basically, get rid of anything that could possibly cause a memory leak or eat cpu cycles behind the scenes. Yes, it takes cpu cycles to invoke a dispose method, but trust me. It is much easier and much less computationally expensive to explicitly dispose of objects, rather than burn time, computation resources, and budget tracking down leaks and performance issues.

Rule #2: If you don't have to do it, don't do it

Another good rule to live by is that if you don't have to do something, then don't do it. No, I don't mean "don't do your work", or "don't brush your teeth". You need to do those. Instead, I mean don't perform computationally intensive, or resource consuming actions if you don't need to.

4) Handle collections properly - There are a few things I see all the time, and they're always the first things to change if I see them. Collections (ArrayCollection, XMLListCollection, etc...) are helper classes that wrap primitive structures like array or xmllist. In order to help make working with those primitives easier, the collection classes do things that can be computationally expensive if you aren't aware of them. The reason that bindings to collection work is because every time you add, remove, or update an item, events get dispatched. They also get dispatched every time you refresh a collection.

The first tip is to be conscious of collection events. If you loop over a collection and update 100,000 items, 100,000 events will get dispatched. This can cause massive performance implications, and can completely lock your application UI. If you don't need those collection events to be dispatched, you can use the disableAutoUpdate() function to suspend collection events. Just be sure to turn them back on when you are done, or if you need them again, using the enableAutoUpdate() function.

The second, is to not use a collection if you don't have to. If all you need is to do loop over 100,000 items, and you arne't using data bindings, then use an array.

And the third tip on collections is only when collections are filtered using a filter function... If a filter function is applied, you don't need to call the refresh() function every time you add a new object to the collection. This can cause performance hits, in some of the least expected places. For example, if you have a datagrid bound to a collection, and have another process which updates that collection. If there is a filter on the collection, it will automatically get filtered when you call the collection's addItem method. Calling the refresh() method after adding an item will cause the list data of the datagrid to be invalidated, thus causing the entire datagrid to be re-validated and re-drawn. This is easily missed when optimizing, and can make drastic changes in application performance.

5) Use deferred instantiation - By default, all navigational containers within Flex (tab nav, accordion, viewstack, etc...) only create their children as they are needed. This prevents the application from creating thousands of components that aren't needed yet, which helps keep the application running smooth, and without hogging resources or locking up. Changing the creation policy can cause big problems if you are not careful.

You should also keep deferred instantiation in mind when creating your own custom components. Don't create child objects in the constructor. Instead, override the createChildren() method, and create them in there. This way, your components also follow deferred instantiation rules, and they don't introduce any performance issues that can be difficult to track down.

6) Object recycling vs new objects - I've written about this one before, but I'll say it again. It is often less expensive to reuse existing objects, rather than creating new ones. This goes hand-in-hand with data virtualization.

7) Don't invalidate/destroy/re-validate your objects if nothing changed.
If you are building custom components, and someone changes a property (through a getter/setter), don't invalidate the component properties if the incoming value didn't change. This will cause the component to go through the full invalidation/validation lifecycle, causing properties to be re-validated/comitted, and the object to be redrawn on the display list. Only invalidate properties if something actually changed. Here is a straightforward example to demonstrate the concept:

public function set myProperty( value : Number ) : void
{
  if ( _myProperty != value )
  {
    _myProperty = value;
    propertiesChanged = true;
    invalidateProperties();
    dispatchEvent( new Event( "change" ) );
  } 
}

 

Rule #3: Use the language appropriately

The ActionScript language has features that enable performance... use them.

8) Dynamic/Generic vs. Typed Objects - Dynamic and generic objects certainly have their place. They are generic, flexible, can be modified with any attribute, and can be used in a wide variety of situations. However, if you have a typed object and do not need the generic qualities, use strongly typed objects. The strongly typed nature of ActionScript 3 is one of the reasons it is fast. Accessing properties of strongly typed objects is simply faster than accessing properties of generic & dynamic objects.

9) Use constants when applicable - If you have a value that does not ever change, but you reference it all the time, use a constant. Constants are accessed faster and require less overhead.

10) Use static members - Static properties and functions do not require a variable instance to be invoked or accessed. Since they don't require an instance, it is faster to access them directly from the class, and does not require the memory necessary to instantiate the object. Utility functions, or functions that do not require attributes of a specific instance should be put into static functions.

Speaking of constants in #9... normally I suggest to make constants static. This will help you keep your memory footprint to a minimum b/c it will not need to be attached to a class instance.


All of these may seem like trivial or minor coding practices, but believe me, they can add up. In reality, it always boils down to proper coding practice, and this list highlights just a few things that you should consider when building your applications.

___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com

 

o-link : http://www.insideria.com/2009/09/10-tips-for-flex-application-p.html

分享到:
评论

相关推荐

    Flex Application Performance: Tips and Techniques

    本文旨在深入解析《Flex Application Performance: Tips and Techniques》这一资料中的关键知识点,帮助读者理解如何有效地提升Flex应用的性能。 #### 执行摘要 《Flex Application Performance: Tips and ...

    Parsley Framework 和 Tips for Flex 的 PPT

    而“Tips for Flex.ppt”则关注Flex开发的最佳实践和技巧。Flex是Adobe推出的一种用于构建富互联网应用程序(RIA)的开源框架,基于ActionScript和MXML。在这个PPT中,你将发现如何优化Flex应用性能,如减少SWF大小...

    Unite Europe 2017 - Squeezing Unity: Tips for raising performance

    Unite Europe 2017 - Squeezing Unity: Tips for raising performance 就是关于如何提升性能的 YouTube 对应的 https://www.youtube.com/watch?v=_wxitgdx-UI&index=7&list=PLX2vGYjWbI0Rzo8D-vUCFVb_hHGxXWd9j

    Pro PHP Application Performance

    Pro PHP Application Performance will help you understand all the technologies and components which play a role in how well your applications run. When seconds can mean the difference between retaining...

    Top 10 Tips for Optimizing SQL Server Performance

    #### 10. 我们试图解决的问题是什么? - **目标**:如何从SQL Server部署中获得最大价值? - **核心问题**:我们是否正以最佳效率运行?应用程序能否顺利扩展? - **可扩展性定义**:一个可扩展的系统是指随着...

    100 power tips for fpga designers

    ### 《100 Power Tips for FPGA Designers》核心知识点概览 #### 一、书籍简介与背景 《100 Power Tips for FPGA Designers》是一本由Evgeni Stavinov撰写的专为FPGA设计人员提供的实用指南。本书版权为2011年,...

    6 Tips For Speaking Natural English_Speak English With Vanessa

    6 Tips For Speaking Natural English_Speak English With Vanessa

    Oracle Hyperion Chapter 2 - Tips for building your HFM Application

    Oracle Hyperion Chapter 2 - Tips for building your HFM Application

    Lua Performance Tips.rar

    "Lua Performance Tips"这份文档深入探讨了如何通过遵循一些编程准则和技巧来提升Lua程序的执行效率。以下是对这些要点的详细解析: 1. **理解Table的运作机制** Lua中的Table是其核心数据结构,用于实现数组、...

    机器学习 -- Tips for Deep Learning

    Tips for Deep Learning Do not always blame Overfitting Hard to get the power of Deep ... Vanishing Gradient Problem ReLU Maxout RMSProp

    Accelerating MATLAB Performance 1001 tips to speed up MATLAB programs

    Accelerating MATLAB Performance: 1001 tips to speed up MATLAB programs By 作者: Yair M. Altman ISBN-10 书号: 1482211297 ISBN-13 书号: 9781482211290 Edition 版本: 1 出版日期: 2014-12-11 pages 页数: ...

    45-tips-database-performance-tips-for-developers

    ### 数据库性能优化技巧 #### 引言 随着信息技术的发展,数据库已经成为现代应用程序不可或缺的一部分。在实际开发过程中,为了确保数据库高效稳定运行,开发者需要掌握一系列优化技巧。本文将详细介绍45个数据库...

    Flex Solutions Essential Techniques For Flex 2 And 3

    Marco takes you through the workings of the example step by step, and then presents some expert's tips, which will take your understanding further, and give you unique insights into Flex development....

    100 power tips for FPGA designers

    本书标题《100 power tips for FPGA designers》明确指出了目标读者群体——FPGA(现场可编程门阵列)设计师。此书旨在向他们提供100个实用的设计技巧,帮助设计师们在工作中更有效率、更优化地利用FPGA。 FPGA是...

    Accelerating MATLAB Performance - 1001 Tips to Speed Up MATLAB Programs

    Accelerating MATLAB Performance - 1001 Tips to Speed Up MATLAB Programs MATLAB 程序加速技巧 http://undocumentedmatlab.com/books/matlab-performance Accelerating MATLAB Performance: 1001 Tips to ...

    Java.EE.7.Performance.Tuning.and.Optimization.178217642X

    Design guidelines for better enterprise application performance Discover different Java optimization tips and tricks Tune a real-world sample application Approach The book adopts a step-by-step ...

    Tips for prospective and early-stage PhD students

    标题中的“Tips for prospective and early-stage PhD students”表明这是一个针对有意攻读或正在早期阶段进行博士学位的学生的指导性资料。ICLR 2020(International Conference on Learning Representations 2020...

    Flex Builder Plug-in and Adobe

    This article delves into the use of the Flex Builder plug-in within the Eclipse environment, focusing on its setup, key features, and how it can be utilized to compile applications for Flash Player 10...

    Oracle.Database.10g.Performance.Tuning.Tips.and.Techniques.

    本篇文章的标题及描述均为 "Oracle.Database.10g.Performance.Tuning.Tips.and.Techniques." 这表明文章的核心内容是关于 Oracle Database 10g 的性能调优技巧和技术。Oracle Database 10g 是一款业界领先的数据库...

Global site tag (gtag.js) - Google Analytics