【问题标题】:Can Java Garbage Collector (GC) collectable for hierarchical object structure? [duplicate]Java 垃圾收集器 (GC) 可以收集用于分层对象结构吗? [复制]
【发布时间】:2018-02-20 07:21:56
【问题描述】:

在下面的类层次结构中,
我将 parentchild 设置为 null。
我不确定垃圾收集器是否可以在 parent 设置为 null 并且 child 已设置为 null 后收集.
(假设 ma​​in 方法在一个类中。)

public class Person {
    String name;
    Person parent;
    Person child;    
    Person (String name) {
        this.name = name;
    }
}

public static void main(String[] args) {
    Person parent = new Person("John");
    parent.child = new Person("Snow");
    parent.child.parent = parent;
    Person child = parent.child;
    parent = null;
    child = null;
    System.out.println("finished");
}

【问题讨论】:

  • GC 比你想象的要聪明得多。

标签: java garbage-collection hierarchical-trees


【解决方案1】:

如果没有其他对这些对象的引用,是的,它们可以被收集。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2011-07-16
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多