Actionscript 内存管理、垃圾收集
此博客(和其他博客)声明您应该设置清理对象时,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从来没有这样做 - 只要你做了显而易见的事情:
然后使用的内存该对象可以随时被覆盖。
I never do - as long as you do the obvious:
Then the memory that was used by the object is available for overwriting at any time.
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.Finally you can use the Janitor class to help monitor/clean up your resources:
http://gskinner.com/libraries/