The responsiveness and performance of a Flex application directly relates to what is being displayed on screen. The more that is happening, the slower the application will respond. In most cases, you won't run into this scenario. If you are dynamically adding LOTS of UIComponents, then this tip will help you create applications that perform very well under heavy load.
One of the most computationally expensive operations in the Flex component life cycle is the constructor and initialization routine. If you are adding and removing lots of complex components, this can become very taxing on the system, and reduce overall application performance. One way to reduce the impact that this has on your system is through object caching, (also known as object pooling).
Flex Bugs related to Memory Leak:
I have provided some of the flex bugs related to the memory leak here (especially in chart components, data binding, itemrenderers etc).
http://bugs.adobe.com/jira/browse/SDK-15710?actionOrder=desc
http://bugs.adobe.com/jira/browse/FLEXDMV-2027
http://bugs.adobe.com/jira/browse/FP-129
Creating WeakReference
Richard Lord over at Big Room Games has an interesting article on hacking AS3 to allow you to create weak-references to objects.
Using the WeakReference hack could be useful if you want to make sure that an object will not stay in
memory if you forget to delete all references to it. However, keeping track of your objects and practicing good memory management is a much better solution and hacks like this one should be saved for special cases where tracking use of an object becomes difficult or impractical.
Reduce, Reuse and Recycle:
when using object caching, you create a cache of objects that the application can use. When the application is finished with these objects, you return the objects to the cache so that they can be reused again. This technique reduces the number of object instances created by the runtime, reduces the overall memory used when running the application, stabilizes the required resources to run the application (less spikes in both cpu and memory), and leads to better overall performance and scalability of the application when applied correctly.
I have created the custom class factory (implements IFactory interface) to implement object caching to generate itemrenderers.
public class CustomRendererFactory implements IFactory
{
Private var objectCache:Array = new Array();
Private var _generator:Class = null;
Public function CustomRendererFactory (generator:Class)
{
This._generator = generator;
}
//Get the object from the ObjectCache if available else create a ne instance.
Public function newInstance():*
{
Var instance:Object = null;
If(ObjectCache.length > 0)
{
Instance = ObjectCache.shift();
}
Else
{
Instance = new _generator();
}
Return instance;
}
//place the object passed as parameter to the objectCache.
Public function setObject(object:*)
{
objectCache.push(object);
}
}
This custom class factory can be used for itemRenderer property of the flex components. After using the renderers make sure that those objects are released by invoking the method setObject of this customRenderer class.
//Create a CustomRendererFactory object passing the class name of the object that we need.
Var renderer: CustomRendererFactory = new CustomRendererFactory(Canvas);
canvasObj:Canvas = Renderer.newInstance() as Canvas;
//
//release the object to cache when it is not in use anymore.
Renderer.setObject(canvasObj);
Renderer = null;
Object caching and the weak reference can be combined to combat the memory leak issues present in the flex application.