我一直在编程在AS3一段时间后发现一个很奇怪的问题与无缘无故被挂在存储串,下面只是节目改变与一个随机字符串的label.text属性,它工作正常,但是当我看着Flex的探查我注意到,弦数steadly增加,我试过执行垃圾收集器但没有帮助我。
这是内存泄漏? 我该如何解决呢?
当我知道这个字符串应该被垃圾收集器收集,因为没有对象引用它们,然而,这是不会发生的所有字符串。
继承人的代码和Flex探查表示字符串的实例的数目的屏幕截图。
<?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
解决了
巴里斯和Loxxy你是对的,我为了找出问题做了一些改变,它长到〜30MB那么垃圾回收器释放一些内存,它永远不会追溯到〜2MB(起点),但图形开始从去〜20MB到30MB〜遍地。
继承人的代码来测试这个
<?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希望以运行一些暂停。