Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an Object eligible for GC even if its instance variable is references to another variable? [duplicate]

I'm studing for the Java Certification 1Z0-803 and I hava a doubt about garbage collection:

import java.util.*;

class X {
    List<String> list = new ArrayList<>();
}

public class TestGC {
    // Is an Object eligible for GC even if its instance variable is references to another variable
    public static void main(String[] args){
        X x = new X(); // 1
        List<String> list = x.list;
        x = null; // 2, Is X object reference eligible for garbage collection here?

        list.add("a");
        list.add("b");
        list.add("c");

        for(String item : list) {
            System.out.println(item);
        }
        list = null;// 3, Or X object reference eligible for garbage collection here, after list is set to null
    }
}

x is referencing the object X created at the position 1.
This class X has a instance variable of the type List.
If I referenced the instance variable list on x of the type X in a local variable list and then set x to null, the object referenced for x will be eligible for GC in this line (position 2) or because I'm referencing an instance variable of this object, this object only be eligible for GC when its instance variable does not have anything reference to it (position 3)?

like image 316
Erick Alves Avatar asked Sep 13 '16 14:09

Erick Alves


People also ask

When an object is eligible for garbage collector?

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null.

Are objects referenced by a variable candidates for garbage collection when the variable goes out of scope?

An object is marked as eligible to be garbage collected when it can no longer be accessed, which can happen when the object goes out of scope. It can also happen when an object's reference variable is assigned an explicit null value or is reinitialized.

At what point is only a single object eligible for GC?

Only objects that have no reference variables referring to them can be eligible for GC.


2 Answers

Initially, you have

stack --> x --> list

x is thus reachable from the stack.

Then you have

stack --> x --> list
     \          /
      \--------/

x is still reachable from the stack, and list is reachable, too, through x, and through the local variable on the stack

Then you set x to null, so you have

 stack      x --> list
     \          /
      \--------/

And you thus see that there is now way to reach x anymore from the stack. The fact that there is a path from x to the list is irrelevant. So the VM is allowed to collect x:

 stack           list
     \          /
      \--------/
like image 79
JB Nizet Avatar answered Oct 12 '22 16:10

JB Nizet


It will be at 2. You aren't referencing the variable of x, you are referencing the List. The fact that x also has a reference to the List does not affect its eligibility for garbage collection. The List is not "inside" x, only a reference to the List is.

The List itself, of course, is not eligible for garbage collection because it is still referenced in the main, but the List and x are independent objects.

like image 25
puhlen Avatar answered Oct 12 '22 18:10

puhlen