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

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

问题描述

我已经编程在AS3一段时间后发现一个非常奇怪的问题,即没有明显的原因是挂在存储器串,以下只是程序改变label.text财产随机字符串,它工作正常但是当我看了看Flex的探查我注意到,弦乐的数量正在增加steadly,我试图执行垃圾收集但是很帮我。

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.

继承人的code和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

解决

巴里斯和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.

继承人的code,以测试这种

Heres the code to test this

<?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()来看看它是否释放这些字符串。但是,你不应该使用在生产code。

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天全站免登陆