2

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.

Community
  • 1
  • 1
cmat
  • 21
  • 1
  • It should go down once the GC gets to it, not a leak. You can try something like this approach to try & reduce the waste http://blogs.adobe.com/pfarland/2007/10/avoiding_string_concatenation.html – VBCPP Apr 29 '14 at 20:38
  • Thank you for your comment. However I implemented it the way you suggest and memory goes on continously growing. The memory rises and never goes down. It looks like a leak. Here there is a link to the [profiler graph](http://imgur.com/fQUAxfw) – cmat May 12 '14 at 16:44

0 Answers0