删除AS3类本身

时间:2011-10-21 20:48:01

标签: flash actionscript-3 actionscript

我有像这样的as3类

package  {

import Global;
import flash.display.MovieClip;
import flash.events.*;
public class Alert extends MovieClip {

    public function Alert(alertTitle:String, alertText:String, alertButton:String = "OK") {
        alert_title.text = alertTitle;
        alert_text.text = alertText;
        alert_button.button_text.text = alertButton;            
        this.x = Global.stage.stageWidth/2;
        this.y = Global.stage.stageHeight/2;
        Global.stage.addChild(this);
        alert_button.addEventListener(MouseEvent.CLICK, Close);
    }

    public function Close(e:MouseEvent){
        this.parent.removeChild(this);
        alert_button.removeEventListener(MouseEvent.CLICK, Close);
    }

}

}

我使用函数Close()删除类本身,但我发现它不会释放内存。有没有办法完全删除它并释放使用过的内存?

抱歉我的英语不好。

5 个答案:

答案 0 :(得分:2)

它是内存管理的,因此不会立即释放对象。垃圾收集器运行后,如果没有任何内容引用Alert,则它将被释放。

答案 1 :(得分:1)

Flash Player使用垃圾收集来释放对象使用的内存。

https://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html

通常,检查内存时不会立即反映删除对象。 Flash Player运行时将确定执行重新分配的适当时间。

答案 2 :(得分:0)

你也失踪了。

public function Close(e:MouseEvent){
  this.parent.removeChild(this);
  alert_button.removeEventListener(MouseEvent.CLICK, Close);
  alert_button=null;
}

它可能无助于您正在寻找的即时内存重新分配,但它将允许GC引擎更快地找到它。

这里还有另一种想法 你拥有的这个警报课程非常小,除非你在舞台上有几百个,你可能根本不会注意到内存的急剧变化

答案 3 :(得分:0)

如果你扩展了MovieClip,你应该为测试添加一些大的bitmapData,当GC删除对象你肯定会看到几个或更多的kb改变。

答案 4 :(得分:0)

首先,您需要确保在单击关闭

后没有任何内容链接到此类

就垃圾收集而言,你可以使用System.gc()作为调试播放器或AIR应用程序,这里是它的文档 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/System.html#gc%28%29

您也可以使用此代码强制进行垃圾回收

try {
    new LocalConnection().connect('foo');
    new LocalConnection().connect('foo');
} catch (e:*) {}
// the GC will perform a full mark/sweep on the second call.

这里有关于它的更多信息http://gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html

相关问题