Friday, February 25, 2011

Be careful with View in Flex 4.5 - Memory leak

Flex 4.5 SDK... I think Adobe did a great job on this. I really do!
However, after i spent 1 week working on Android app based on TabbedViewNavigatorApplication, i noticed that i have a memory leak doing nothing more but just changing selected index of navigator.

In the app i was building, i had 3 views. Two views had some simple lists and one view had a papervision - nothing special, just some basic Cube rendering and animation. The problem here is that each time pushView() method is called, new instance of provided class is created. So if you keep changing the view, you are creating tone of instances. Since in the whole app, i am not calling pushView() by myself, the application does not respond to Keyboard.BACK button. So i could afford to kill the active view  by myself after it is removed from display list.

Here is how it looks when you let navigator to manage the memory on its own:

And here is how it looks when you kill the instances by yourself:

As you can see above, it looks better... I am not sure if it is a bug, but TabbedViewNavigatorApplication has a memory leak for sure.
Here is how i did it:

tabbedNavigator.addEventListener(IndexChangeEvent.CHANGING,onChanging);


private function onChanging(e:IndexChangeEvent):void
{
var ob:Object = application.tabbedNavigator.selectedNavigator.activeView;
ob.destroy();
 ob= null;
}

Of course, destroy function should be defined inside the View you created and this is where you remove all elements, set null where it should be null, etc.

Anyway, its Friday 7:30 PM and i am still sitting at work... Log offff










1 comment:

  1. Anonymous12:27 PM

    This is still in a "private" beta for a reason.

    ReplyDelete