How does Java Garbage Collection work with Circular References?(Java 垃圾收集如何与循环引用一起使用?)
问题描述
据我了解,Java 中的垃圾回收会清理一些对象,如果没有其他对象指向"该对象.
From my understanding, garbage collection in Java cleans up some objects if nothing else is 'pointing' to that object.
我的问题是,如果我们有这样的事情会发生什么:
My question is, what happens if we have something like this:
class Node {
public object value;
public Node next;
public Node(object o, Node n) { value = 0; next = n;}
}
//...some code
{
Node a = new Node("a", null),
b = new Node("b", a),
c = new Node("c", b);
a.next = c;
} //end of scope
//...other code
a、b 和 c 应该被垃圾回收,但它们都被其他对象引用.
a, b, and c should be garbage collected, but they are all being referenced by other objects.
Java 垃圾收集如何处理这个问题?(或者仅仅是内存消耗?)
How does the Java garbage collection deal with this? (or is it simply a memory drain?)
推荐答案
如果无法通过从垃圾收集根开始的链访问对象,Java 的 GC 会将其视为垃圾",因此将收集这些对象.即使对象可能相互指向形成一个循环,但如果从根部切断它们仍然是垃圾.
Java's GC considers objects "garbage" if they aren't reachable through a chain starting at a garbage collection root, so these objects will be collected. Even though objects may point to each other to form a cycle, they're still garbage if they're cut off from the root.
请参阅 Java 平台中的附录 A:垃圾收集的真相中关于不可访问对象的部分性能:战略和战术 用于血淋淋的细节.
See the section on unreachable objects in Appendix A: The Truth About Garbage Collection in Java Platform Performance: Strategies and Tactics for the gory details.
这篇关于Java 垃圾收集如何与循环引用一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 垃圾收集如何与循环引用一起使用?
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
