-1

In the following class hierarchical structure,
I set null to parent and child.
I'm not sure whether Garbage Collector can collect or not after parent has been set as null and child has been set as null.
(Assuming that main method is inside a class.)

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");
}
Nano Man
  • 41
  • 4

1 Answers1

-1

If there are no other reference to any of these objects, yes, they can be collected.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24