在kinetic.js中删除和销毁有什么区别 [英] What is the difference between remove and destroy in kinetic.js

查看:115
本文介绍了在kinetic.js中删除和销毁有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关KineticJS Docs的文档,它说:

I read the documentation on KineticJS Docs and it says that:

destroy() - 删除并销毁自我

destroy() - remove and destroy self

remove() - 从父项中删除self,但不要销毁

remove() - remove self from parent, but don't destroy

如果我理解的是正确的,则remove()函数会从父项中删除该节点但仍然在内存中分配。而destroy()完全释放了内存,这是正确的吗?有人可以用非专业术语解释,如果我使用destroy over remove会有什么并发症吗?

If what I understand is correct, the remove() function removes the node from the parent but is still allocated in the memory. Whereas destroy() completely frees the memory, is that correct? Can somebody explain in layman terms what are some complications if I used destroy over remove?

提前谢谢你。

最温暖的问候,
Dandy Ling

Warmest Regards, Dandy Ling

推荐答案

我相信你是对的,但只是为了补充你说的话我认为你会使用:

I believe you are right, but just to add to what you said I would think that you would use:

remove() - 删除一个节点,以后可能会使用该节点

remove() - to remove a node and possibly use that node later

destroy() - 如果您不再需要此节点,则完全销毁节点

destroy() - to destroy a node completely if you know you won't need this node any longer

此外,以下是垃圾收集的一些优点和缺点直接来自维基: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science %29

Also, here are some benefits and disadvantages to Garbage Collection taken directly from the wiki: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29


好处

Benefits

垃圾收集解放了程序员手动处理内存释放。因此,某些类别的错误被消除或大幅减少:

Garbage collection frees the programmer from manually dealing with memory deallocation. As a result, certain categories of bugs are eliminated or substantially reduced:


  • 悬空指针错误,当一块内存被释放时发生仍有指向它的指针,其中一个指针被解除引用。到那时,内存可能已被重新分配给另一个用户,结果不可预测。

  • 双重免费错误,当程序试图释放已释放的内存区域时发生可能已经再次分配了。

  • 某些类型的内存泄漏,其中程序无法释放已无法访问的对象所占用的内存,这可能导致内存耗尽。 (垃圾收集通常不处理可达的数据的无限累积,但程序实际上不会使用它。)

  • 持久数据结构的高效实现
    垃圾收集解决的一些错误可能会产生安全隐患。

  • Dangling pointer bugs, which occur when a piece of memory is freed while there are still pointers to it, and one of those pointers is dereferenced. By then the memory may have been re-assigned to another use, with unpredictable results.
  • Double free bugs, which occur when the program tries to free a region of memory that has already been freed, and perhaps already been allocated again.
  • Certain kinds of memory leaks, in which a program fails to free memory occupied by objects that have become unreachable, which can lead to memory exhaustion. (Garbage collection typically does not deal with the unbounded accumulation of data that is reachable, but that will actually not be used by the program.)
  • Efficient implementations of persistent data structures Some of the bugs addressed by garbage collection can have security implications.

缺点

通常,垃圾收集有一些缺点:

Typically, garbage collection has certain disadvantages:


  • 垃圾收集在决定释放哪个内存时消耗计算资源,即使程序员可能有已经知道这个信息。为了方便在源代码中手动注释对象生存期的代价是开销,这可能导致性能降低或不均匀。与内存层次结构效应的交互可能会使这种开销在难以预测或在常规测试中检测到的情况下无法忍受。

  • 实际收集垃圾的时刻可能无法预测,导致停顿分散在整个会话中。不可预测的停顿在实时环境,事务处理或交互式程序中是不可接受的。增量,并发和实时垃圾收集器通过不同的权衡来解决这些问题。

  • 非确定性GC与基于RAII的非GCed资源管理不兼容。因此,对非GCed资源的显式手动资源管理(发布/关闭)的需求变得对组合具有传递性。也就是说:在非确定性GC系统中,如果资源或类似对象的资源需要手动资源管理(发布/关闭),并且该对象被用作另一个对象的部分,则组合对象也将成为像对象这样的资源本身需要手动资源管理(发布/关闭)。

这里还有另一个来自SO的参考:垃圾收集机制如何运作?

And also here's another reference from SO: How does the Garbage Collection mechanism work?

这篇关于在kinetic.js中删除和销毁有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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