本以为调用了SWFLoader::unloadAndStop()方法,内存就会像注释中写的那样释放。结果System.totalMemory还是在不停的忘上跳。
下面是当时的主要代码:
protected function addBtn_clickHandler(event:MouseEvent):void
{
var swf:SWFLoader = new SWFLoader();
swf.addEventListener(Event.COMPLETE,swf_completeHandler);
swf.load('xxx.swf');
}
protected function removeBtn_clickHandler(event:MouseEvent):void
{
var swf:SWFLoader = SWFLoader(ctner.getElementAt(0));
swf.unloadAndStop(true);
ctner.removeAllElements();
}
protected function swf_completeHandler(event:Event):void
{
var target:SWFLoader = SWFLoader(event.target);
target.removeEventListener(Event.COMPLETE,swf_completeHandler);
ctner.addElement(target);
}
后来查看了unloadAndStop代码才明白:
public function unloadAndStop(invokeGarbageCollector:Boolean = true):void
{
useUnloadAndStop = true;
unloadAndStopGC = invokeGarbageCollector;
source = null; // this will cause an unload unless autoload is true
if (!autoLoad)
load(null);
}
结论:
调用unloadAndStop()时要注意autoLoad的值,
1)如果swfloader不在显示列表了,而SWFLoader::autoLoad 又为true,内存不会释放,这时要在调用前把autoLoad设为false。上面的“source = null”要在控件下一帧更新时调用commitProperties()时才会起作用,因为控件已从显示列表中删除所以不会触发。
2)如果swfloader一直在显示列表中,不管autoLoad是何值都会起作用。