Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview question: Objects eligible for garbage collection

Give the following code:

class A {
    Boolean b;
    A easyMethod(A a){
        a = null;
        return a;
    }
    public static void main(String [] args){
        A a1 = new A();
        A a2 = new A();
        A a3 = new A();
        a3 = a1.easyMethod(a2);
        a1 = null;
        // Some other code 
    }
}

The question is how many objects are eligible for garbage collection right before // Some other code.

Then correct answer is (at least that's the interviewer answer): 2 - the Boolean b because it's a wrapper and a1 .

Can you please me explain me why a2 and a3 aren't being garbage collected ?

LATER EDIT:

  • Ok, I think I get it now. It was a bit confusing at first, but now i am sure the interviewer was wrong. My initial mistake was that at first I didn't consider that Java is pass by value only, so it's impossible to make a2 null from inside a function that take "a2" as a parameter, because that a2 is actually a copy of a2.
  • The part with the Boolean b was indeed quite obvious.

Thanks for an answer, I will send some interview feedback after that :).

like image 372
Andrei Ciobanu Avatar asked Jul 21 '10 13:07

Andrei Ciobanu


People also ask

Which objects are eligible for garbage collection?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.

How many objects are eligible for garbage collection after this line?

as per the answer given for this question, there is no object eligible for GC at line 11; but according to me at least one object, t2, which is set to point null at line 6, should be eligible for garbage collection.

What is the main objective of garbage collection?

The main objective of Garbage Collector is to free heap memory by destroying unreachable objects.

How many objects are eligible for garbage collection after execution of line 5?

There is only one object which is eligible for garbage collector 4) . Two objects are eligible 5) . The number of objects eligible for GC is two.


1 Answers

Assuming go is supposed to be easyMethod it works like this

class A {
    Boolean b;
    A easyMethod(A a){
        a = null; // the reference to a2 was passed in, but is set to null
                  // a2 is not set to null - this copy of a reference is!
        return a; // null is returned
    }
    public static void main(String [] args){
        A a1 = new A(); // 1 obj
        A a2 = new A(); // 2 obj
        A a3 = new A(); // 3 obj
        a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
        a1 = null; // so far, a1 and a3 have been set to null and flagged
        // Some other code 
    }
}

Two objects are eligible for garbage collection (a1 and a3). b is not because it's only a reference to null. No Boolean was ever made.

To get around the inane subtleties of what // Some other code might be, I instead posit the question be reworded into the following:

Prdict and explain the following output:

class A {
    int i;
    A(int i) { this.i = i; }
    public String toString() { return ""+i; }
    A go(A a){
        a = null; // the reference to a2 was passed in, but is set to null
                  // a2 is not set to null - this copy of a reference is!
        return a; // null is returned
    }
    public static void main(String [] args){
        A a1 = new A(1); // 1 obj
        A a2 = new A(2); // 2 obj
        A a3 = new A(3); // 3 obj
        a3 = a1.go(a2); // a3 set to null and flagged for GC - see above for why
        a1 = null; // so far, a1 and a3 have been set to null and flagged

        test(a1);
        test(a2);
        test(a3);

    }
    static void test(A a) {
        try { System.out.println(a); } 
        catch(Exception e) { System.out.println((String)null); }
    }
}

And output:

c:\files\j>javac A.java

c:\files\j>java A
null
2
null

And the followup is that at that point, a1 and a3 were eligible for GC, and a2 was not.

The lesson from this question is that "Passing an object reference to a method and setting that reference to null does not cause the original reference to be nulled". That's the piece of knowledge the interviewer was attempting to test.

like image 79
corsiKa Avatar answered Sep 21 '22 01:09

corsiKa