垃圾引用和悬空引用有什么区别?
垃圾引用和悬空引用有什么区别?
What is the difference between garbage and dangling references?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
垃圾引用和悬空引用有什么区别?
What is the difference between garbage and dangling references?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
悬空引用是对不再存在的对象的引用。垃圾是无法通过引用到达的对象。
垃圾收集语言中不存在悬空引用,因为对象仅在不再可访问时才会被回收(仅收集垃圾)。在某些语言或框架中,您可以使用“弱引用”,它可以悬空,因为在收集过程中不会考虑它们。
在手动内存管理的语言中,例如 C 或 C++,您可能会遇到悬空指针,例如这样做:
A dangling reference is a reference to an object that no longer exists. Garbage is an object that cannot be reached through a reference.
Dangling references do not exist in garbage collected languages because objects are only reclaimed when they are no longer accessible (only garbage is collected). In some languages or framework, you can use "weak references", which can be left dangling since they are not considered during collection passes.
In languages with manual memory management, like C or C++, you can encounter dangling pointers, by doing this for instance:
悬空引用是对不再存在的对象的引用。
什么被视为垃圾取决于垃圾收集器的实现。
对于跟踪和引用计数 GC,不会存在悬空引用(除非存在 GC 实现错误),因为只有那些不存在引用的元素才被视为有资格进行垃圾回收。
因此,悬空引用几乎只对于具有手动内存管理的系统来说是一个问题。
A dangling reference is a reference to an object that doesn't exist anymore.
What is considered garbage depends on the implementation of your garbage collector.
With both tracing and reference counting GCs, dangling references cannot exist (unless there's a GC implementation bug) because only those elements are considered eligible for garbage collection to which no reference exists.
Thus, dangling references are an issue pretty much only for systems with manual memory management.
悬空引用:对最初分配但现在已释放的内存地址的引用
垃圾:已在堆上分配且尚未显式释放但程序无法访问的内存。
Java 有一个垃圾收集器。它及时删除悬空指针和其他垃圾内存。
Dangling Reference: Reference to a memory address that was originally allocated, but is now deallocated
Garbage: Memory that has been allocated on the heap and has not been explicitly deallocated, yet is not accessible by the program.
Java has a garbage collector. It timely removes dangling pointers and other garbage memory.