删除 AS3 类本身

发布于 2024-12-11 04:33:23 字数 839 浏览 1 评论 0原文

我有这样的 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() 来删除类本身,但我注意到它不会释放内存。有什么方法可以完全删除它并释放使用的内存吗?

抱歉我的英语不好。

I have as3 class like this

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

}

}

I use function Close() to remove class itself but i noticed it doesn't frees memory. Is there any way to remove it completely and free used memory?

Sorry for my bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空气里的味道 2024-12-18 04:33:23

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

It's memory managed, so the objects won't get freed immediately. After the Garbage collector runs, if nothing is referencing Alert then it will be freed.

爱,才寂寞 2024-12-18 04:33:23

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

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

一般来说,删除一个对象在检查内存时不会立即反映出来。 Flash Player 运行时将确定执行释放的适当时间。

Flash Player uses Garbage Collection for deallocating memory used by objects.

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

Generally, removing an object will not immediately be reflected when inspecting memory. Flash Player runtime will determine the appropriate time to perform the deallocation.

记忆之渊 2024-12-18 04:33:23

你也失踪了。

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

它可能对您正在寻找的即时内存重新分配没有帮助,但它会让 GC 引擎更快地找到它。

这里还有另一个想法
您拥有的这个 Alert 类非常小,除非您在舞台上有几百个这样的警报类,否则您可能根本不会注意到内存的巨大变化

you are also missing.

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

It might not help the instant memory reallocation that you are looking for but it will allow the GC engine to find it faster.

Also another thought here
This Alert class you have is extremely small and unless you have a few 100 of them on stage you probably won't notice a drastic change in memory at all

云之铃。 2024-12-18 04:33:23

如果你扩展了MovieClip,你应该在里面添加一些大的位图数据来进行测试,当GC删除对象时你肯定会看到很少或更多的kb变化。

If You extends MovieClip , You should for tests add some big bitmapData inside , when GC remove object You will for sure see few or more kb change .

遇见了你 2024-12-18 04:33:23

首先,您需要确保单击关闭后没有任何内容链接到此类

。 至于垃圾收集,您可以将 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

First you need to make sure that nothing is linking to this class after you click the close

As far as garbage collection goes, you can use System.gc() for the debug player or for AIR app, here is the documentation for it
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/System.html#gc%28%29

You could also use this code to force garbage collection

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

here is more info about it http://gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文