【问题标题】:casalib and garbage collection?casalib 和垃圾收集?
【发布时间】:2010-12-22 21:35:05
【问题描述】:

我整理了一个快速示例,只是为了测试 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;
    }
}
}

这是我的电影剪辑类:

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();
    }

}
}

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

【问题讨论】:

    标签: flash garbage-collection


    【解决方案1】:

    我决定从舞台和我的所有事件侦听器中删除所有内容(基本上是一个空的 Flash 电影)并只添加儿童(new Stats());

    仅此一项就占用了 34KB 的内存并逐渐增加,所以我想这是我移除并销毁其他所有内容后内存“爬行”的原因...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多