垃圾收集最佳实践

发布于 2024-08-22 23:29:47 字数 137 浏览 5 评论 0原文

如果您要从显示列表中删除某个 MovieClip,并且该 MovieClip 又具有具有自己的事件侦听器的子 MovieClip,则是否有必要从子 MovieClip 中删除所有侦听器?

或者只是直接从显示列表中删除的父级 MovieClip?

If you're removing a MovieClip from the display list, and that MovieClip in turn has child MovieClips which have their own event listeners, is it necessary to remove ALL listeners from the child MovieClips?

or just the parent MovieClip that is being directly removed from the display list?

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

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

发布评论

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

评论(2

酒浓于脸红 2024-08-29 23:29:47

这取决于附加到父级或子级 MovieClip 的侦听器是否有指向它的弱引用。

添加监听器时,可以使用最后一个参数来设置监听器是否使用弱引用。这正是您提出的问题需要了解的内容。

//This listener will use a weak reference, notice the last "true"
something.addEventListener("event", myFunction, false, 0, true);
//This is called a weak reference listener.
//The ussual listener, with default parameters, is a strong refence listener.

EventDispatcher 参考

如果删除对该对象的所有引用,该对象将被垃圾收集。使用默认参数添加的侦听器将计入这些引用(因为最后一个参数默认值为 false)。因此,如果 MovieClip 或其任何子级附加了强引用侦听器,则通过将其从显示列表中删除,剪辑将不会被垃圾回收,直到侦听器引用也被删除(通过使用 removeEventListener 方法)。

如果您在剪辑或其任何子级中使用弱引用,则通过将其从显示列表中删除,它最终将被垃圾收集。请记住,这可能会在一段时间后发生,因此像 ENTER_FRAME 这样的事件可能仍然会被触发和调用,直到对象最终被收集为止。

It depends if the listeners attached, to either the parent or children MovieClips, have weak references pointing to it or not.

When you add a listener, you can use the last parameter to set if the listener will use a weak reference. This is exactly what you need to know for the question you ask.

//This listener will use a weak reference, notice the last "true"
something.addEventListener("event", myFunction, false, 0, true);
//This is called a weak reference listener.
//The ussual listener, with default parameters, is a strong refence listener.

EventDispatcher Reference

So an object will be garbage collected if all the references to such object are deleted. Listeners added with the default parameters count toward those references (since the last parameter default value is false). So having a MovieClip with a strong reference listeners attached to it or any of its children, by removing it from the display list the clip will NOT be garbage collected until the listener references are also deleted (by using the removeEventListener method).

If you use weak references in a clip or any of its children, by removing it from the display list it will eventually be garbage collected. Have in mind that this may occur after some time, so events like ENTER_FRAME may be still be triggered and called until the object is finally collected.

梦回旧景 2024-08-29 23:29:47

我认为删除Child(MC)就足够了,然后MC=null或delele MC,我想删除将完成您从Adobe阅读规​​范的工作,我认为您也可以调用System.gc,但这适用于AIR应用程序。

I think is enough to removeChild(MC), then MC=null or delele MC, I guess delete will do the work id you read the specifications from Adobe, I think you can also call System.gc but this is for AIR apps.

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