casalib和垃圾收集?

时间:2010-12-22 21:35:05

标签: flash garbage-collection

我整理了一个快速示例,以测试casalib中的垃圾收集便利功能。

所有似乎都运行良好(即,从舞台上移除对象,监听器被停止)但是我使用的是Doob先生的统计数据显示,看起来我的内存使用率在调用destroy方法后没有下降?

这是我的文档类:

package {
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import net.hires.debug.Stats;
import org.casalib.display.CasaMovieClip;
import flash.events.MouseEvent;

public class GarbageCollectionExample extends CasaMovieClip {

    public var clean:MovieClip = new MovieClip();
    public var garbage:CasaMovieClip = new CasaMovieClip();

    public function GarbageCollectionExample() {
        addChild(new Stats());
        clean.buttonMode = true;
        clean.mouseChildren = false;
        clean.addEventListener(MouseEvent.CLICK, onClean, false, 0, true);
    }


    private function onClean(e:MouseEvent):void
    {
        garbage.destroy();
        garbage = null;
        clean.removeEventListener(MouseEvent.CLICK, onClean);
        removeChild(clean);
        clean = null;
    }
}
}

这是我的movieclip课程:

package {
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import net.hires.debug.Stats;
import org.casalib.display.CasaMovieClip;
import flash.events.MouseEvent;

public class Garbage extends CasaMovieClip {

    public var timer1Display:TextField = new TextField();
    public var timer2Display:TextField = new TextField();
    public var eventChangeDisplay:TextField = new TextField();
    public var clean:MovieClip = new MovieClip();

    private var timer1count:int = 0;
    private var timer2count:int = 0;
    private var eventChangeCounter:int = 0;

    private var firstTimer:Timer;
    private var secondTimer:Timer;

    public function Garbage() {
        startEvents();
        addEventListener(Event.CHANGE, onEventChange, false, 0, true);
    }

    private function startEvents():void
    {
        firstTimer = new Timer(1000);
        firstTimer.addEventListener(TimerEvent.TIMER, onFirstTimer, false, 0, true);
        firstTimer.start();

        secondTimer = new Timer(200);
        secondTimer.addEventListener(TimerEvent.TIMER, onSecondTimer, false, 0, true);
        secondTimer.start();
    }

    private function onFirstTimer(e:TimerEvent):void
    {
        timer1Display.text = "first timer has played " + timer1count;
        timer1count++;
        dispatchEvent(new Event(Event.CHANGE));
    }

    private function onSecondTimer(e:TimerEvent):void
    {
        timer2Display.text = "second timer has played " + timer2count;
        timer2count++;
        dispatchEvent(new Event(Event.CHANGE));
    }

    private function onEventChange(e:Event):void
    {
        eventChangeDisplay.text = "event has changed " + eventChangeCounter + " times";
        eventChangeCounter++;
    }

    override public function destroy():void
    {
        firstTimer.stop();
        secondTimer.stop();
        removeEventListeners();
        super.destroy();
    }

}
}

我的物品是否真的被标记为垃圾收集?我做错了什么?

1 个答案:

答案 0 :(得分:0)

我决定从舞台和所有我的事件监听器(基本上是一个空的flash电影)中删除所有内容,并且只删除addChild(new Stats());

单独占用34KB的内存并逐渐增加,所以我想在我移除并摧毁其他所有内容后,这可以解释我的记忆“爬行”......