as3:数组拼接是否完全删除一个对象?
我在一个数组中有很多显示对象,我不断地在舞台上添加和删除这些对象。移除后,它们将不再使用。
考虑到 displayObject 不在显示列表中,并且没有事件侦听器...如果我使用 splice 将其从数组中删除,它会被垃圾收集吗?
如果我先将对象清空,会更安全吗?
myArray[2] = null;
myArray.splice(2,1);
I have a lot of display objects in an array that I'm constantly adding and removing from stage. When removed they are not used anymore.
Considering the displayObject is not on the display list, and has no event listeners... will it be garbage collected if I use splice to remove it from the array?
Is it safer if I null the object first?
myArray[2] = null;
myArray.splice(2,1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要没有对
DisplayObject
的剩余引用,就可以使用splice
将其从数组中删除,甚至只是将其设置为null
将使其成为垃圾收集的候选者。更新:在从数组中删除之前将项目设置为
null
是多余的,并且不会产生任何影响。As long as there are no remaining references to the
DisplayObject
then yes, removing it from the array usingsplice
, or even just setting it tonull
will allow it to become a candidate for garbage collection.Update: Setting the item to
null
before removal from the array is redundant and will make no difference.如果您需要更好的性能,我建议您使用池而不是创建大量对象并将它们放入数组中。使用一些链表实现代替 splice() 操作,这确实很慢。
If you need better performance I suggest you using pools instead of creating lots of objects and putting them in array. Use some linked list implementation instead of splice() operation, which is really slow.