Select to view content in your preferred language

Using Map class programmatically in AS3 causes memory leaks

1205
2
08-16-2011 05:36 AM
ChristophSchramm
Emerging Contributor
Hi,

we are developing a rather large application that dynamically displays some maps with varying layer configurations.
In the code I have variables like:

var mapCanvas:Group;
var map:Map;

In some function I instantiate the map and add it to the group like:

map = new Map();
mapCanvas.addElement(map);

then add layers to the map. Everything works quite fine.
Now (in some other function) I do the following:

map.removeAllLayers();
map.layers = null;
map.lods = null;
mapCanvas.removeElement(map);
map = null;

The problem I have is that the instance of the Map class is not collected by the garbage collector. Also a lot of LOD, SpatialReference and Extent classes stay in memory. It seems that some internal references in the Map class are not cleared up and no functionality to do this manually is exposed. This causes a considerable memory leak after a while.

Example: After instantiating the map, say, 10 times with different layer configurations, I have something like 10 Map instances in memory, also 914 LOD instances, 226 SpatialReference instances and 135 Extent instances.
Those are never collected by the garbage collector.

Also, I cannot reuse the instance of the Map class because after calling the removeAllLayers() function, setting a new layer on the map with a different spatial reference does not work.

Am I missing somethig here? Any thoughts on this are very much appreciated.


Chris
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Chris,

   I could be off base here but since the map is a UI component and you have it added to the canvas will that not maintain a strong reference to the map component and thus remain in memory?  Have you tried to remove it from the canvas and then set it to null?
0 Kudos
ChristophSchramm
Emerging Contributor
Robert,

thanks for the reply. Yes of course I am removing the map from the canvas. Nevertheless I found the solution and of course it is my fault and a big D'OH moment: I didn't remove an event listener from a custom layer that I added to the map, thus preventing the layer to be garbage collected and also the map that this layer was registered with. Morale: ALWAYS remove event listeners from classes that are supposed to go out of scope...
One question remains though (dear FLEX API team): Why can we not sort of reset the Map class and re-initialize it with different (tiled) layers that have a different spatial reference?

Chris
0 Kudos