Java Programming - Garbage Collections - Discussion


public class X 
    public static void main(String [] args) 
        X x = new X();
        X x2 = m1(x); /* Line 6 */
        X x4 = new X();
        x2 = x4; /* Line 8 */
    static X m1(X mx) 
        mx = new X();
        return mx;
After line 8 runs. how many objects are eligible for garbage collection?

[A]. 0  
[B]. 1
[C]. 2
[D]. 3

Answer: Option B


By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.


Amit Sharma said: (Mar 28, 2012)  
But mx still holding the reference created in m1 method....since it is assign to both mx and x2 is having same reference x4 is assign to means now only mx is holding the reference Why this is eligible for garbage collection ?

Basha said: (Sep 24, 2012)  
@Amit Sharma.

The scope of the mx reference variable only within the m1() method.
After executing the m1() metho the garbage collection remove the mx variable form memory. Hence the object that is created by m1() is only eligeble for garbage collecion after executing the 8th line.

Amardeep Burnwal said: (Oct 28, 2012)  
As in the method mx is the local variable so after the method finishes we can't access mx. So it will go to garbage collector (gc). Now at line 6 x2 loses its previous reference. So I think total two are eligible for gc. Can anyone tell me how I'm wrong according to the solution given.

Lestat said: (Nov 8, 2012)  
public class JavaScopeTest {

public int num = 0;

public static void main(String[] args){
JavaScopeTest x = new JavaScopeTest();
JavaScopeTest x2 = m1(x); /* Line 6 */
JavaScopeTest x4 = new JavaScopeTest();
x2 = x4; /* Line 8 */

static JavaScopeTest m1(JavaScopeTest mx)
mx = new JavaScopeTest();
mx.num = 10;
return mx;

This print:


It seem that the reference for x2 was lost, they are two elements elegible for gc.

I think so.

Gubari said: (Jun 14, 2016)  
Hi, when you pass parameters to method, the truth is:

a) Objects are passed by reference.

b) Primitives are passed by value.

The reason of result is different than explained in question answer. In line.

mx = new StaticClass();

we create the new object, and in next line, we return it to x2. Finally, we have two different objects x and x1.

Tim said: (Jan 20, 2017)  
The answer is clearly wrong based on the fact that it says:

Remember that "Java is pass by value, " so the reference variable x is not affected by the m1() method.

Uwe said: (Mar 9, 2017)  
Excuse me, but I've learned, that an' object is immediately visible for the garbage collector, after the last line it was mentioned.

Object variables are pointers in the stack, which point to objects in a heap. After line 8 we've got x, x2, x4 and mx. mx is already dereferenced, x and x2 are 2 pointers which point to the same address after line 6, after line 8, x2 points to the same object as x4. Though after line 8 they are not touched anymore, we should have 3 objects visible to the gc.

Please, could anyone explain, where I'm wrong? Thanks

Surendra J said: (Nov 20, 2017)  
This program is show compilation error when using doComplexStuff();.

Ken said: (Feb 17, 2018)  

the answer is correct.

While Java is pass by value, it passes the values of the references by value not the actual objects. Like when you call a method add(int a, int b) you don't expect the original a and b to have changed right? Same as when you pass objects, the references are passed by value. You can use those references to change the underlying objects but you can't dispose of the objects themselves with a direct reference, as such it is what Gubari explained. Line 6 is then effectively "X x2 = new X();" from there it is clear.

Ani said: (Jan 10, 2019)  
Please anyone clearly explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.