I have a Flex code in which I assign values to a string variable many times. I would expect the garbage collector to deallocate the memory associated to previous assignments, but it does not seem to do so. The memory with the profiler grows more and more every time I do the assignment (by clicking the image, which calls onMouseDown), and not only the first time. Besides it never decreases.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="doInit()">
<fx:Script>
<![CDATA[
var backup:String;
private function doInit():void {
img.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
}
private function onMouseDown(event:MouseEvent):void {
backup=getSubStr();
System.gc();
}
private function getSubStr():String {
var subStr:String = new String;
var x:Number=Math.random();
for (var h:int=0;h<8000;h++)
subStr+=x.toString();
return subStr;
}
]]>
</fx:Script>
<s:Image id="img" source="@Embed(source='imagenes/mapa.png')"/>
</s:Application>
In the complete code, I don't call the garbage collector, this is just done here for the testing.
I've read (in AS3 String Memory Leak) that Flash uses Master Strings.As the profiler string loitering objects point to the line "subStr+=x.toString();" I think that this concatenation of strings may be related to the problem. However I have no idea on how to prevent the leak.
Any feedback would be appreciated.