Page 1 of 2

About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Dec 23, 2012 4:24 pm
by Svetopolk
After which line will the object created at line XXX be eligible for garbage collection?

public Object getObject(Object a) //0
{ Object b = new Object();  //XXX
Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return c; //4
}
I think that all local variables become eligible for garbage collection when the block is finished.
A method is over after a return statement. So in line 4 method (just after) getObject is finished and all local variables become eligible for garbage collection.

Where am i wrong?

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Dec 23, 2012 4:47 pm
by admin
An object may become garbage collected even before end of a method, which is what this question is getting at.

In other words, an object created locally is eligible to be GCed at the end of the method, but that is not necessarily the point where it becomes eligible. The point where an object becomes eligible for GC could be a lot earlier than that as well.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Dec 23, 2012 6:31 pm
by Svetopolk
admin wrote:.. could be a lot earlier than that as well...
agree, but back to the question: all options earlier doesn't fit. So .... ????

P.S
Correct answer: Never in this method.
My answer: after line 4

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Dec 23, 2012 6:54 pm
by admin
Svetopolk wrote:
admin wrote:.. could be a lot earlier than that as well...
agree, but back to the question: all options earlier doesn't fit. So .... ????

P.S
Correct answer: Never in this method.
My answer: after line 4
I see what you mean.
Technically, the objects become eligible after their references go "out of scope" (if there are no other references to them, of course). While it may seem like a return statement would end the scope, it is not so. The scope ends with a closing } and not with a return.

Therefore, "Never in this method" is the right option.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Mon Dec 24, 2012 7:30 am
by Svetopolk
1 more idea. "return c " pass an object outside the method (a link to an object).
After "}" all variables are marked to "destroy" but the object will live outside the method.
Probably it is correct explanation. what do you think?

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Mon Dec 24, 2012 7:38 am
by admin
Svetopolk wrote:1 more idea. "return c " pass an object outside the method (a link to an object).
After "}" all variables are marked to "destroy" but the object will live outside the method.
Probably it is correct explanation. what do you think?
Yes, that is correct and that is what the explanation already explains.
Note that at line 2, c is assigned the reference of b. i.e. c starts pointing to the object created at //XXX. So even if at //3 b and a are set to null, the object is not without any reference.
Also, at //4 c is being returned. So the object referred to by c cannot be garbage collected in this method!
If there is a reference, then there is no question. It just cannot be GCed.

Since you mentioned in your original post "all local objects". I was talking from the perspective where there is no reference to a local object. When is that collected - after return statement or after closing }?

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Mon Dec 24, 2012 8:25 am
by Svetopolk
Thanks.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Wed Feb 20, 2013 6:54 am
by alex
Hi,

I agree with the answer: Never in this method.
But on the other hand the answer "Cannot be determined" is also correct because we actually don`t know from context where and when it will be eligible for garbage collection.
So there are two ambiguous answers.
Does it sound reasonable?

Thanks in advance,
Alex

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Fri Feb 22, 2013 3:53 am
by alex
Hi,

Seems like you have missed my question)

Alex

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Fri Feb 22, 2013 6:43 am
by admin
There is no ambiguity in "Never in this method". No matter what happens in other code, it will not be GCed in this method.
HTH,
Paull

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Fri Feb 27, 2015 7:06 am
by inaelrodrigues
After which line will the object created at line XXX be eligible for garbage collection?

Code: Select all

public Object getObject(Object a) //0    {
  Object b = new Object();  //XXX  
  Object c, d = new Object(); //1 
  c = b; //2 b = a = null; //3 
  return c; //4 
}

Why in line "b = a = null; //3" is note marked for garbage collection?

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Fri Feb 27, 2015 7:49 am
by admin
I am sorry, I did not understand your question. Can you please rephrase?

thank you,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Thu Jul 23, 2015 7:49 am
by Roibeard
After which line will the object created at line XXX be eligible for garbage collection?
After (not on) line 4 is when the method is closed. Isn't that what releases resources for collection?


//thanks

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sat Jan 16, 2016 1:24 pm
by GuillaumeBailly
Hi,

I get your point @inaelrodrigues. Honestly, I chose the same answer. For me, after line 3, the references a, b and c point to null, so the object itself is eligible for garbage collection from that point (line 3) and not later.

Regards,
Guillaume

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sat Jan 16, 2016 9:47 pm
by admin
Well, the object will be available for GC only after the assignment b = a = null; is executed not before. If you say, at line //3, it could also mean just before b = a = null; because that is also technically line marked //3.

The point is, the whole statement b = a = null; is at line //3 so it would be incorrect to say that the object will be eligible for gc at that line. Only thing that is certain is that right after line //3, it will be eligible for GC.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Jan 17, 2016 5:01 am
by GuillaumeBailly
Hi Paul,

Ok, thanks for your answer. Since the question says "After which line...", if I say "line3" I suppose I'm correct too.

Regards,
Guillaume

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sun Jan 17, 2016 5:13 am
by admin
Sorry, I was looking at the wrong code. I checked this question again and the given answer I.e. "never in this method", is correct. Please go through the given explanation.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Tue Mar 07, 2017 1:55 pm
by lenalena
GuillaumeBailly wrote:Hi,

I get your point @inaelrodrigues. Honestly, I chose the same answer. For me, after line 3, the references a, b and c point to null, so the object itself is eligible for garbage collection from that point (line 3) and not later.

Regards,
Guillaume
c doesn't point to null, it points to the object created in line XXX.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sat May 27, 2017 12:53 pm
by Kevin30
Hi Paul,

From the question:
Object c, d = new Object(); // 1
Can you tell me if this line creates 2 different objects (with c and d pointing to two seperate objects) or does this create just 1 object (with c and d pointing to the same object).

I think it creates 2 different objects and not 1. Am I correct?

Thank you in advance!

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sat May 27, 2017 1:03 pm
by admin
It creates only one object. Further, only d points to that object and not c. c is null.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Wed Aug 30, 2017 6:44 pm
by shambhavi
public int getObject(Object a) //0
{
Object b = new Object(); //XXX
Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return 0; //4
} // 5

in this code, when is XXX eligible for garbage collection ? after line 5 ?
can you also tell me , once control is abruptly returned back after line 4 back to the caller, when actually is the scope of this method over ?

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Wed Aug 30, 2017 9:04 pm
by admin
Scope of a method is always from opening bracket to closing bracket.
Object will be eligible to be GCed after line //4 itself. The difference between //4 and //5 is not important for the purpose of the exam.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Thu Aug 31, 2017 12:58 am
by shambhavi
after line 4 anyways the control is transferred back to the caller, which means that we are out of the scope of the method after line 4 ,

and hence we say as long as we are in/within the method , the object is never eligible for GC .
Am I right ? :)

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Thu Aug 31, 2017 2:38 am
by admin
That is correct.

Re: About Question enthuware.ocajp.i.v7.2.919 :

Posted: Sat Sep 02, 2017 2:48 pm
by shambhavi
thank you !

and one more clarification .
if instead of return c, if we had return (c=null);

then on this line itself the object would become eligible for garbage collection right ? i.e. after the expression (c=null ) is evaluated . since before the full statement return (c=null); is complete, when (c=null ) is evaluated itself its eligible for GC.

Am I right ?