在查看代码后我怀疑/好奇会有任何内存泄漏,所以我自己测试了你的代码。
您发布的代码实际上无法运行。我刚刚添加了最低限度,让它像 app 标签和 import 语句一样运行(以及一个计数器标签,用于查看 http 服务周期何时完成):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="application1_creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.rpc.http.HTTPService;
import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
private var xmlC:XMLListCollection = new XMLListCollection();
private var httpS:HTTPService = new HTTPService();
private var timer:Timer = new Timer(1000);
private var xmlData:XML;
private var xmlDataList:XMLList;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
httpS.url = "data.xml";
httpS.addEventListener(ResultEvent.RESULT, resultHTTP);
httpS.resultFormat="e4x";
httpS.send();
timer.start();
timer.addEventListener(TimerEvent.TIMER, updateXMLC);
}
private function updateXMLC(event:TimerEvent):void
{
xmlC.source = xmlDataList;
httpS.send();
}
private function resultHTTP(event:ResultEvent):void
{
counter.text = Number(parseInt(counter.text,10)+1).toString();
xmlData = event.result as XML;
xmlDataList = xmlData.dg.rows.row;
}
]]>
</mx:Script>
<mx:Label id="counter" text="0" horizontalCenter="0" verticalCenter="0" fontSize="72"/>
</mx:Application>
好消息是您的代码中没有任何内存泄漏。探查器中也没有任何游荡对象。
坏消息是您在应用程序中所做的任何其他事情(代码未在此处发布,无论出于何种原因省略) - 这就是您有泄漏/游荡对象的地方。
您可以在附加代码的分析器中亲自看到,在 http 服务的前几个周期后内存没有增加。即它不会随着时间的推移继续掌握更多的记忆。(顺便说一下,我要提取的 XML 文件大约有 8000 行,超过 1MB)。
如果您想发布更多代码,很乐意进一步查看 - 但现在认为这解决了这个谜团。;)
以下是您所做的一些修复:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="application1_creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.rpc.http.HTTPService;
import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;
import mx.events.FlexEvent;
[Bindable] private var xmlC:XMLListCollection = new XMLListCollection();
private var httpS:HTTPService = new HTTPService();
private var timer:Timer = new Timer(1000);
private var xmlData:XML;
private var xmlDataList:XMLList;
private var serviceRunning : Boolean = false;
private var currentData : String = '';
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
httpS.url = "data.xml";
httpS.addEventListener(ResultEvent.RESULT, resultHTTP);
httpS.resultFormat="e4x";
httpS.send();
timer.start();
timer.addEventListener(TimerEvent.TIMER, updateXMLC);
}
private function updateXMLC(event:TimerEvent):void
{
xmlC.source = xmlDataList;
if( !serviceRunning ){ // don't call for more data until
httpS.send(); // you've gotten back last call
}
}
private function resultHTTP(event:ResultEvent):void
{
// make sure we have differences before rebinding
var newData : String = event.result as String;
serviceRunning = false;
counter.text = Number(parseInt(counter.text,10)+1).toString();
if( newData != currentData ){
xmlData = event.result as XML;
currentData = newData;
xmlDataList = xmlData.dg.rows.row;
}
}
]]>
</mx:Script>
<mx:Label id="counter" text="0" horizontalCenter="0" verticalCenter="0" fontSize="72"/>
</mx:Application>