AS3 字符串内存泄漏 [英] AS3 String Memory Leak

查看:29
本文介绍了AS3 字符串内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 AS3 中编程了一段时间,发现一个非常奇怪的问题,字符串没有明显的原因挂在内存上,下面的程序只是用一个随机字符串更改了 label.text 属性,它工作正常但是当我查看 Flex 分析器时,我注意到字符串的数量在稳步增加,我尝试执行垃圾收集器但没有帮助我.

I've been programming for some time in AS3 and found a really weird problem with strings that for no apparent reason are hanging on the memory, the program below just changes the label.text property with a random string, it works fine but when i looked at the Flex profiler i noticed that the number of Strings is increasing steadly, i tried executing the garbage collector but didnt helped me.

这是内存泄漏吗?我该如何解决?

Is this a memory leak? how can i solve it?

据我所知,垃圾收集器应该收集这些字符串,因为没有引用它们的对象,但并非所有字符串都会发生这种情况.

As I know this strings should be collected by the garbage collector because there are no objects referencing them, yet this is not happening for all the strings.

这是显示 String 实例数量的 Flex 分析器的代码和屏幕截图.

Heres the code and a screenshot of the Flex profiler showing the number of String instances.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
<s:layout>
    <s:BasicLayout/>
</s:layout>
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        protected var t:Timer=new Timer(10);

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            t.addEventListener(TimerEvent.TIMER,listener,false,0,true);
            t.start();
        }

        protected function listener(e:Event):void
        {
            var s:String=Math.random()+"-->";
            this.fx(s);
            s=null;
        }

        protected function fx(s:String):void
        {
            this.label.text=s;
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="label" y="39" left="10" right="10" text="Label"/>
</s:WindowedApplication>

抱歉,不到 10 分,这里是分析器截图http://imageshack.us/a/img11/9716/stackw.png

Sorry, less than 10 points, heres the profilers screenshot http://imageshack.us/a/img11/9716/stackw.png

已解决

Baris 和 Loxxy 你是对的,我做了一些改变以隔离问题,它增长到 ~30Mb 然后垃圾收集器释放一些内存,它永远不会回到 ~2mb(起点)但图表开始一遍又一遍地从 ~20mb 到 ~30mb.

Baris and Loxxy you were right, i made some changes in order to isolate the problem and it grows up to ~30Mb then the garbage collector frees some memory, it never goes back to ~2mb (starting point) but the graph starts to go from ~20mb to ~30mb over and over.

这是测试这个的代码

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
<s:layout>
    <s:BasicLayout/>
</s:layout>
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        protected var maxMemoryUsage:Number=0;
        protected var i:Number=0;

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            setTimeout(Fx,20);
        }

        protected function Fx():void
        {
            if(i++%1024==0) 
            {
                var mem:Number=System.totalMemory;
                this.maxMemoryUsage = mem>this.maxMemoryUsage?mem:this.maxMemoryUsage;
                trace(this.maxMemoryUsage + ' / ' + mem);
            }

            var s:String="";
            s+=Math.random().toString()+"qwertyuiu...1024 random chars...iiwqe";
            this.aSimpleString=s;
            setTimeout(Fx,20);
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <fx:String id="aSimpleString"/>
</fx:Declarations>
</s:WindowedApplication>

同样调用 System.gc() 什么也没做,也许 gc 需要一些暂停才能运行.

Also calling to System.gc() did nothing, maybe the gc expects some pause in order to run.

推荐答案

垃圾收集器会随时运行.通常它发生在分配新对象时,但如果内存使用率不高,则在您的情况下可能不会发生.

The garbage collector is going to run whenever it feels like. Usually it happens on allocation of new objects but it might not happen in your case if the memory usage is not high.

您可以尝试调用 System.gc() 以查看它是否释放了这些字符串.但是您不应该在您的生产代码中使用它.

You can try to call System.gc() to see if it frees those strings. But you shouldn't use that in your production code.

有关详细信息,请参阅答案.

See this answer for more info.

这篇关于AS3 字符串内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆