View the demo here
There is a lot of confusion over memory management and garbage collection in Flex and Flash. So determining if you have a memory leak is a difficult thing to do, that's why at Kap Lab we worked on developing a memory leak inspector plug-in which can be loaded by KapInspect a component intended to help all Flex, AS3 and Flash developers to introspect their applications.
This tool is a kind of "profiler light", dedicated to memory leaks watching.
The principle of this watcher is to use a Dictionary of objects, with the "useWeakKeys" parameter set to true. The objects being watched are referenced through a LeakWatcher object stored in that Dictionary with the object watched being the "weak key" in this dictionary.
So, when the object is released from memory, it is also automatically released from the dictionary, and - as we refresh the list - it disappears from the list as well.
This works only when we do a garbage collection, which we force in autorefresh mode (and this also slows application), or when you manually hit refresh.
However, event when using the System.gc() call, objects are not always removed immediately, and sometime you have to wait a little to see objects disappear from the list.
In spite of these shortcomings, the tool is useful as it is much more easy to use than the Flex Profiler (especially for BIG applications...).
In order to start inspecting your application for memory leak you have to integrate KapInspect and to do so I invite you to read the Integration Guide.
Once you have integrated KapInspect all you have to do now is to copy "KapInspectMemoryModule.swf" in your binary directory and to load the plug-in while you are instantiating KapInspect.
Integration into MXML
<mx:Application xmlns:mx=http: <kapit:KapInspect pluginModules="KapInspectMemoryModule" />
Integration with ActionScript code
<mx:Application xmlns:mx=http: <mx:Script>
private function onCreactioComplete():void
var kap:KapInspect = new KapInspect();
kap.pluginModules = "KapInspectMemoryModule";
MemoryLeak Inspector contains two tabs, the first one the overview tab which contains the objects being instantiated in the application and the second one the leaks tab which contains the different instances of the object in the application and from which the user can inspect the different objects.
In this tab the different objects being inspected are added to the grid by category with the number of instances of the object. By category we refer to the class name (Button, ComboBox ...), or to category manually registered (see below)
The Refresh button and the autorefresh check box forces the garbage collection which allows verifying the memory consumed by the objects. It updates the data in the grid allowing knowing which objects are still instantiated.
The autowatch check box tells the inspector to inspect all the object being added to stage for memory leak (UIComponent). Be cautious with this option, as it will significantly slow down the application. This option is useful, however, for automatically grabbing added UIComponent and watch for leaks.
In this tab the user can select any of the categories in the combo box that have been added in the grid in the overview tab and the different instances of the selected category will be than displayed in the grid.
The user can select an instance in the grid and he can add a strong reference to the object by clicking on the grabObjects button. The user will be then able to select an instance of the object and select from the contextual menu one of the different inspector available in KapInspect.
When using this function, you create strong references to every objects displayed in the grid, to they will never be reclaimed after that. This option is useful to inspect leaking objects, and to help identify them and finding the leak
We are not able to detect when a popup has been created, as no event is dispatched (at least, we never found them !)
So for watching popup leaks, you will have to add them manually to the MemoryLeak watch list, by using a single line of code described below:
private function _onInitialize():void
Once the PopUp is initialized a new entry under the name TestPopup will be added to the overview tab in the MemoryLeakInspector. The user will be then able to inspect memory leak like any other object.
The method described to add leak watch manually can be used to add leaks watch to any object as follow:
Of course, for production code, you will either remove these instructions, either put them into conditional compilation instructions.