Java Programming - Garbage Collections - Discussion

Discussion Forum : Garbage Collections - General Questions (Q.No. 5)
5.
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 */
        doComplexStuff();
    }
    static X m1(X mx) 
    {
        mx = new X();
        return mx;
    }
}
After line 8 runs. how many objects are eligible for garbage collection?
0  
1
2
3
Answer: Option
Explanation:

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.

Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Discussion:
10 comments Page 1 of 1.

Ani said:   7 years ago
Please anyone clearly explain.

Ken said:   8 years ago
@Tim.

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.

Surendra j said:   8 years ago
This program is show compilation error when using doComplexStuff();.

Uwe said:   8 years ago
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

Tim said:   9 years ago
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.

Gubari said:   9 years ago
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.

Lestat said:   1 decade ago
public class JavaScopeTest {

public int num = 0;

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

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

This print:

0
10
0

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

I think so.

Amardeep Burnwal said:   1 decade ago
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.
(1)

Basha said:   1 decade ago
@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.

Amit Sharma said:   1 decade ago
But mx still holding the reference created in m1 method....since it is assign to x2.....so both mx and x2 is having same reference .....now x4 is assign to x2....it means now only mx is holding the reference ....so Why this is eligible for garbage collection ?

Post your comments here:

Your comments will be displayed after verification.