Actionscript 内存管理、垃圾收集

发布于 2024-11-05 05:56:25 字数 377 浏览 1 评论 0原文

此博客(和其他博客)声明您应该设置清理对象时,在 dispose() 方法中对象引用 null。

但是,Actionscript 3(带有 Flash Player 9)使用标记和清除 为您清除循环引用。所以我想知道:是否真的有理由取消对象引用?

This blog (and others) state that you should set object references to null inside your dispose() methods when cleaning up objects.

However, Actionscript 3 (with Flash Player 9) uses mark and sweep to clear out circular references for you. So I am wondering: is there really any reason to null out your object references?

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

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

发布评论

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

评论(2

慢慢从新开始 2024-11-12 05:56:25

我从来没有这样做 - 只要你做了显而易见的事情:

  • 打破对对象的所有引用(从数组中删除,将存储对象的变量设置为空,从显示列表中删除)
  • 删除所有事件侦听器等等

然后使用的内存该对象可以随时被覆盖。

var ar:Array = [];
var mc:MovieClip = new MovieClip();

mc.addEventListener(MouseEvent.CLICK, pants);

ar[ar.length] = mc;
addChild(mc);

if(mc.parent) mc.parent.removeChild(mc); // not garbage collected
mc.removeEventListener(MouseEvent.CLICK, pants); // still not garbage collected
ar.splice(0, 1); // finally garbage collected

I never do - as long as you do the obvious:

  • Break all reference to the object (remove from arrays, set variables storing the object to null, remove from display list)
  • Remove all event listeners and so on

Then the memory that was used by the object is available for overwriting at any time.

var ar:Array = [];
var mc:MovieClip = new MovieClip();

mc.addEventListener(MouseEvent.CLICK, pants);

ar[ar.length] = mc;
addChild(mc);

if(mc.parent) mc.parent.removeChild(mc); // not garbage collected
mc.removeEventListener(MouseEvent.CLICK, pants); // still not garbage collected
ar.splice(0, 1); // finally garbage collected
一曲爱恨情仇 2024-11-12 05:56:25

Grant Skinner 的演讲对内存管理进行了精彩的总结:

http://gskinner.com/talks/resource-管理/

总而言之,我从不null对象本身,而是将引用它们的对象设为null(有一个微妙但重要的区别)。不过,需要销毁该对象的所有引用以及事件侦听器等。

添加事件侦听器时,请养成将侦听器设置为弱侦听器的习惯。

o.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);

这没有什么缺点,并且意味着如果您将所有对对象的引用设为 null o但仍然有附加的侦听器,它们会自行删除,并且该对象仍然可以标记为GC'ed。尽管如此,您仍然应该自行处理侦听器的删除。

“不要偷懒——事后清理
你自己!”

,您可以使用 Janitor 类来帮助监视/清理您的资源:

http://gskinner.com/libraries /

A fantastic summary of memory management is the Grant Skinner presentation:

http://gskinner.com/talks/resource-management/

In summary though, I never null the objects themselves, but null the objects referencing them (there is a subtle but important difference). All references need to the object need to be destroyed though, and also event listeners, etc.

When adding event listeners, get into the habit of setting the listener to be weak.

o.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);

There is no disadvantage to this, and means that if you null all references to your object o but there is still listeners attached, they will remove themselves, and the object can still be marked for gc'ed. You should still handle your own removal of listeners regardless though.

"don't get lazy - clean up after
yourself!"

Finally you can use the Janitor class to help monitor/clean up your resources:

http://gskinner.com/libraries/

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