Tuesday, July 7, 2009

Flex/ Flash Performance Optimization

Here are few techniques and tips which can be used to increase performance/speed of the flex application. These are generic steps which can be used by any flex/flash(AS3.0) applications.

Reduce Memory Utilization: "garbage collection" implies that objects no longer needed by the program are "garbage" and can be thrown away. When an object is no longer referenced by the program, the heap space it occupies can be recycled so that the space is made available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. Here are few methods which can help in optimizing application.
1.1 Remove event listeners when there role is done. Generally Loader, URLLoader, are few class which generally we don't remove.
1.2 Instantiate Array when it is no longer needed. It is easy way to free memory. If array contains objects/array as item then application will
Eg. a = new Array();
1.3 Call gc() after some regular. It forces the Flash VM to explicitly free the memory.
1.4 Keep watch on Bitmap and BitmapData object this object occupy huge memory.
1.5 Object/Variable Typing: Always remember to type the object to correct type; avoid using variables without type. It is bad habit is to use the Object class. In ActionScript 3 the AVM2can make use of defined objects that are not dynamic.
---------------------------------------------------------------------


Iterations Vector[ms] Object[ms]

---------------------------------------------------------------------
10^2 0.1 0.1
10^3 1:3 2:2
10^4 13:3 17:2
10^5 73:4 109:2
10^6 672:8 1033:7

---------------------------------------------------------------------
From above table we can see that time complexity of the object increases exponential index. So try using int instead of Number for loop counters.
SLOW Version
var obj: Object = new Object();
obj.x = 1;
obj.y = 2;
FAST VERSION

class MyMatrix{
public var x:Number;
public var y: Number;
}
var mat:MyMatrix = new MyMatrix();

mat.x = 1;
mat.y = 2;
MORE FAST VERISON

class MyMatrix{
public var x:int;
public var y:int;
}

var mat:MyMatrix = new MyMatrix();
mat.x = 1;
mat.y = 2;