When I have an object, removing all references to it is enough to sign it up for the garbage collector, atleast that's what I heard. Eg.:
当我有一个对象时,删除对它的所有引用就足以为垃圾收集器签名了,至少我听到了。例如。:
removeChild(object);
object = null;
I'm still a bit confused though, because does this mean that the event listeners made inside this object's instance are also automatically removed? Or is there anything I need to do?
我仍然有点困惑,因为这是否意味着在这个对象的实例中制作的事件监听器也会被自动删除?或者我需要做什么?
2 个解决方案
#1
Ah, you've hit on the crux of memory management in managed code: if you're an object, and you have a reference to another object (even if it's only in the form of an event listener), then you are at least one reason that object won't be removed from memory during a GC.
啊,你已经在托管代码中找到了内存管理的关键:如果你是一个对象,并且你有另一个对象的引用(即使它只是以事件监听器的形式),那么你至少在GC期间不会从内存中删除对象的一个原因。
For display objects, and in my experience pretty much anytime you want to subscribe to an event dispatcher but not be responsible for that dispatcher's remaining in memory, you should add your event listener with the weak reference option:
对于显示对象,根据我的经验,您几乎想要订阅事件调度程序但不负责该调度程序剩余的内存,您应该使用弱引用选项添加事件监听器:
myPublisher.addEventListener("myEvent", myHandlerFunction, false, 0, true);
In just about every situation I encounter these days, "false, 0, true" (where true means "use weak reference," and which translates loosely as "add this listener, but don't make it a reason for the dispatcher not to get cleared from memory" -- see the docs for more information) is the proper way to add event listeners. Very few textbooks or documentation snippets illustrate this approach for some reason, which is unfortunate, because it makes for a much more intuitive memory-management experience. I'd suggest using it as the rule rather than the exception.
在我遇到的几乎所有情况中,“假,0,真”(其中true表示“使用弱引用”,并且松散地翻译为“添加此侦听器,但不要让调度员不要将其作为原因”从内存中清除“ - 查看文档以获取更多信息”是添加事件侦听器的正确方法。很少有教科书或文档片段出于某种原因说明这种方法,这是不幸的,因为它可以提供更直观的内存管理体验。我建议使用它作为规则而不是例外。
Hope it helps!
希望能帮助到你!
#2
an important thing you should consider is that if an object O has eventhandlers, i.e. methods that were added as eventhandlers to E, then this also count as a reference ... unless you add the handler with weakreference ...
一个重要的事情你应该考虑的是,如果一个对象O有事件处理程序,即作为事件处理程序添加到E的方法,那么这也算作一个引用...除非你用弱引用添加处理程序...
also if you have a closure, whose outer scope contains a reference to O, then this is a reference ...
如果你有一个闭包,其外部作用域包含对O的引用,那么这是一个引用...
you do not need to remove all references to O, to sign it up for GC either ... the GC also removes circular references ... it removes all "islands" in memory, if you will ... only "peninsulae" connected of the "continent" of builtin objects, i.e. Timers, the display list, I/O layer etc. are not collected ...
您不需要删除对O的所有引用,也可以为GC注册... GC也会删除循环引用...它会删除内存中的所有“孤岛”,如果您将...仅“peninsulae”连接内置对象的“大陆”,即定时器,显示列表,I / O层等未被收集...
the last paragraph of another post of mine deals with this topic a little ...
我的另一篇文章的最后一段涉及这个主题......
so yeah, basically, if O gets GCd, then any event handlers get GCd, unless there is another reference to them ... etc.
所以是的,基本上,如果O获得GCd,那么任何事件处理程序都会获得GCd,除非有另一个对它们的引用......等等。
hope that helped .. :)
希望有帮助.. :)
greetz
back2dos
#1
Ah, you've hit on the crux of memory management in managed code: if you're an object, and you have a reference to another object (even if it's only in the form of an event listener), then you are at least one reason that object won't be removed from memory during a GC.
啊,你已经在托管代码中找到了内存管理的关键:如果你是一个对象,并且你有另一个对象的引用(即使它只是以事件监听器的形式),那么你至少在GC期间不会从内存中删除对象的一个原因。
For display objects, and in my experience pretty much anytime you want to subscribe to an event dispatcher but not be responsible for that dispatcher's remaining in memory, you should add your event listener with the weak reference option:
对于显示对象,根据我的经验,您几乎想要订阅事件调度程序但不负责该调度程序剩余的内存,您应该使用弱引用选项添加事件监听器:
myPublisher.addEventListener("myEvent", myHandlerFunction, false, 0, true);
In just about every situation I encounter these days, "false, 0, true" (where true means "use weak reference," and which translates loosely as "add this listener, but don't make it a reason for the dispatcher not to get cleared from memory" -- see the docs for more information) is the proper way to add event listeners. Very few textbooks or documentation snippets illustrate this approach for some reason, which is unfortunate, because it makes for a much more intuitive memory-management experience. I'd suggest using it as the rule rather than the exception.
在我遇到的几乎所有情况中,“假,0,真”(其中true表示“使用弱引用”,并且松散地翻译为“添加此侦听器,但不要让调度员不要将其作为原因”从内存中清除“ - 查看文档以获取更多信息”是添加事件侦听器的正确方法。很少有教科书或文档片段出于某种原因说明这种方法,这是不幸的,因为它可以提供更直观的内存管理体验。我建议使用它作为规则而不是例外。
Hope it helps!
希望能帮助到你!
#2
an important thing you should consider is that if an object O has eventhandlers, i.e. methods that were added as eventhandlers to E, then this also count as a reference ... unless you add the handler with weakreference ...
一个重要的事情你应该考虑的是,如果一个对象O有事件处理程序,即作为事件处理程序添加到E的方法,那么这也算作一个引用...除非你用弱引用添加处理程序...
also if you have a closure, whose outer scope contains a reference to O, then this is a reference ...
如果你有一个闭包,其外部作用域包含对O的引用,那么这是一个引用...
you do not need to remove all references to O, to sign it up for GC either ... the GC also removes circular references ... it removes all "islands" in memory, if you will ... only "peninsulae" connected of the "continent" of builtin objects, i.e. Timers, the display list, I/O layer etc. are not collected ...
您不需要删除对O的所有引用,也可以为GC注册... GC也会删除循环引用...它会删除内存中的所有“孤岛”,如果您将...仅“peninsulae”连接内置对象的“大陆”,即定时器,显示列表,I / O层等未被收集...
the last paragraph of another post of mine deals with this topic a little ...
我的另一篇文章的最后一段涉及这个主题......
so yeah, basically, if O gets GCd, then any event handlers get GCd, unless there is another reference to them ... etc.
所以是的,基本上,如果O获得GCd,那么任何事件处理程序都会获得GCd,除非有另一个对它们的引用......等等。
hope that helped .. :)
希望有帮助.. :)
greetz
back2dos