动作3:做自我事件侦听器prevent被垃圾收集的对象? [英] Actionscript 3: Do self event listeners prevent an object from being garbage collected?

查看:125
本文介绍了动作3:做自我事件侦听器prevent被垃圾收集的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,事件侦听器和引用对象将prevent垃圾回收处理的对象。我的问题是,将一个事件侦听器放置在对象上,听着同一个对象,prevent被垃圾收集的对象?

I know that event listeners and references to an object will prevent the garbage collector from dealing with objects. My question is, will an event listener placed on an object, listening to the same object, prevent that object from being garbage collected?

我问,因为它看起来像,这是发生在我身上。我破坏我所有的对象引用,但我仍然看到痕迹当定时器熄灭。与此同时,好像这不应该prevent的集合,因为它会造成分配但unreferenceable内存。

I ask because it seems like this is happening to me. I am breaking all my references to an object but I am still seeing traces when its timer goes off. At the same time, it seems like this should not prevent the collection because it would create allocated but unreferenceable memory.

推荐答案

在AVM2的垃圾收集器的应该的发现不可达的对象。但由于垃圾回收具有不确定性,这是非常困难的依靠,甚至测试这种行为 - 这可能是因为垃圾收集器可以正常使用,但不打扰,因为你有自由足够的内存来运行标记和清除

No, but.

The AVM2 garbage collector is supposed to find unreachable objects. But because garbage collection is non-deterministic, it's very hard to rely on or even test this behaviour -- it could be that the garbage collector is working perfectly, but not bothering to run the mark-and-sweep since you have enough RAM free.

这是一个好主意,删除事件侦听器,当你与对象就大功告成了,即使他们是从对象本身(即循环引用)。为什么这是一个好主意吗?由于你永远不知道什么时候垃圾收集器将运行。如果你想确定的行为,总是以确定的方式删除监听器,尤其的像计时器和ENTER_FRAME时间敏感的事件,否则你创建你的听众运行,垃圾收集器运行之间的竞争条件。垃圾收集器只是周期性运行。

It's a good idea to remove event listeners when you're done with the object, even if they're from the object itself (i.e. circular references). Why is this a good idea? Because you never know when the garbage collector is going to run. If you want deterministic behaviour, always remove listeners in a deterministic fashion, especially for time-sensitive events like TIMER and ENTER_FRAME, otherwise you're creating a race condition between your listeners running and the garbage collector running. The garbage collector only runs periodically.

在一般情况下,如果你要附加的事件侦听器,而无需创建一个额外的参考对象,通过真实的的addEventListener的将useWeakReference 参数( )。如果你想停止接收的事件马上,不过,你仍然需要尽快你这个对象手动分离的事件侦听器。

In general, if you want to attach event listeners without creating an additional reference to the object, pass true to the useWeakReference parameter of addEventListener(). If you want to stop receiving the events right away, though, you'll still need to manually detach your event listeners as soon as you're done with the object.

这篇关于动作3:做自我事件侦听器prevent被垃圾收集的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆