AdvancedDataGrid with HierarchicalData causing “memory leaks”

I recently ran into an issue with one of my Flex applications which used an AdvancedDataGrid with HierarchicalData that contained several thousand records as its data provider. When the data got refreshed, the application’s memory usage would increase significantly. Eventually this caused the application to use several gigs of memory.

Gigs.

After much Googling I discovered this knowledgebase article from Adobe:

The AdvancedDataGrid control does not clear the openNodes property if you replace the data provider with a new data provider. Because the openNodes property contains references to objects in the old data provider, the objects cannot be removed from memory by the garbage collection mechanism.

Internally, the AdvancedDataGrid control represents hierarchical and grouped data by using the IHierarchicalCollectionView interface. The IHierarchicalCollectionView interface defines  the openNodes property. This property contains an Array of objects that represent the nodes of the data provider that are currently open in the AdvancedDataGrid. In most cases, you can ignore the openNodes property when you update the data provider.

However, if you replace the data provider with a new data provider, the AdvancedDataGrid control does not clear the openNodes property. Because the openNodes property contains references to objects in the old data provider, the objects cannot be removed from memory by the garbage collection mechanism.  To ensure that the old data provider can be removed from memory, use an event handler to clear the openNodes property when you replace the data provider.

 

This is exactly what was happening in my situation. Following the knowledgebase’s example, I added an event listener to my data provider’s CollectionEvent.COLLECTION_CHANGE event which cleared the openNodes property.

// Handle the colectionChange event.
private function removeOpenNodes(event:CollectionEvent):void {
  if(event.kind == CollectionEventKind.RESET)
    IHierarchicalCollectionView(adg.dataProvider).openNodes = {};
}

I’m not sure why Flex keeps the references to the openNodes from the old data provider, but once I cleared the openNodes property each time the data provider got refreshed, my application’s memory usage was much improved.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>