我正在尝试从内存中删除简单对象,但是当我调用removeChildren时,内存使用情况上升了:/我不知道为什么?我该如何删除对象?
package {
import flash.display.DisplayObject;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.system.System;
public class Main extends Sprite {
public function Main()
{
for (var i:int = 0 ; i < 1000;i++) {
var addBouncerButton:SimpleButton = new SimpleButton();
addBouncerButton.x = 100;
addBouncerButton.y = 10;
addBouncerButton.name = "Btn"+i;
addChild(addBouncerButton);
}
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, remove);
}
private function remove(e:KeyboardEvent):void
{
trace("Children : " + this.numChildren);
trace(System.totalMemory * 1024 + " kb");
if(this.numChildren > 0)
var o:DisplayObject = removeChildAt(this.numChildren - 1);
o = null;
}
private function update(event:Event):void
{
}
}
}
答案 0 :(得分:2)
您可以使用delete关键字对对象进行排队以进行垃圾回收。这个垃圾收集器可能需要一段时间(几毫秒)来完成它的工作,并且不应该对您的对象进行尾随引用。
为了更好地理解as3 GC,您可以阅读这篇优秀的文章:http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
祝你好运!答案 1 :(得分:0)
删除对对象的所有引用,例如,如果您有eventlisteners删除它们并将对象设置为null。然后,垃圾收集器将为您清理该对象,而您不必担心它。