aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection question" Watch "Garbage collection question" New topic
Author

Garbage collection question

Geoff Peters
Greenhorn

Joined: Dec 15, 2002
Posts: 4
This may seem trivial but I can't wrap my feeble little brain around it.
When talking about garbage collection, if I am using an object reference ex1 in the following context...
{
Object ex1 = new Object();
// irrelevant code here****
ex1 = null;
// possibly more code here not involving ex1

return ex1;
}
Where and WHY does the object reference become available for garbage collection? My instinct says after the return statement, as it is still in use, but I know the correct answer is after the object reference is set to null. Assuming the code would compile, what would be returned? Wouldn't it be possible for the object reference to be gone from memory before the return statement?
Thanks!!
Geoff
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Geoff -
For certification, terminology regarding the GC is very important. The line of code:
Object ex1 = new Object();
Does 3 things:
1) creates a reference variable 'ex1' that can refer to a object of type Object.
2) Creates an object (on the garbage collectible heap) of type Object.
3) refers 'ex1' to the new object.
When you get to the line :
ex1 = null;
You are dereferencing ex1 from the object. Assuming no other references to the object exist, at this point the object is eligible for garbage collection.
I think that when you talk about the object reference being eligible for garbage collection, you are mixing up concepts. Let's assume that 'ex1' is a local variable (not static, and not an instance variable), in that case, (ex1 being 'local') it lives on the stack, and it lives and dies according to stack rules. The garbage collector has nothing to do with the stack, it only removes abandoned objects from the heap.
If 'ex1' is an instance variable, then the correct answer would be - 'can't tell for sure'. The instance variable lives and dies according to when it's object lives and dies.
So in general, object references (variables) aren't ever, per se, eligible for garbage collection, only objects get garbage collected.
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Neo Xiang
Greenhorn

Joined: Jul 21, 2002
Posts: 8
but what's the final answer?
i think 'cause the return sentence,so it 'll cause gc after the return. is that right?
girish rateshwar
Ranch Hand

Joined: Mar 04, 2001
Posts: 99
Hi,
Well Garbage Collection cannot be forced, even though a reference to null for the object has been made, but it depends on the JVM as to when it will GC.
As far as the above code is concerned, obj=null does nothing as we still have a reference to the obj.
So after the return, it might as well be just eligible for GC.
Regards,


Girish
Geoff Peters
Greenhorn

Joined: Dec 15, 2002
Posts: 4
Girish - That's what I thought, but the one GC question on the exam was similar to that, and that's what I answered (and got it wrong). So I am trying to figure out why that is wrong, as I agree with everything you said in your post! I guess the preferred answer must have been "Can't say for sure if GC occurs"?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
obj = null ???
are we looking at the same code?
Prasad Kuppa
Ranch Hand

Joined: Apr 01, 2002
Posts: 151
think of this :
public Object returnNullObject()
{
Object nullObj = null;
return nullObj;
}
In the above code no object is created and hence no questions about eligibility to GC.
Prasad
Fintan Conway
Ranch Hand

Joined: Apr 03, 2002
Posts: 141
Originally posted by girish rateshwar:
As far as the above code is concerned, obj=null does nothing

Wrong!!!
The line
sets the variable 'obj' to point (or refer) to an object which has been newly created.
The line
causes the variable 'obj' to no longer point to the previously created object. This object now has no variables pointing to it - therefore it is
available for garbage collection.
The line
returns an ordinary variable which refers to null - and has NO connection anymore to the object which had been previously created.
Hope this clears up matters,
Fintan
Ramkumar Venkat
Greenhorn

Joined: Dec 09, 2002
Posts: 7
I think the object referenced by ex1 should be eligible for garbage collection as soon as the variable reference it is set to null, provided no other variable is referencing it at that point of time.
And the reference ex1, being a local variable, should be eligible for garbage collection after the method returns. And the return value of this function will always be null.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Yikes ! this is the strangest thread i've ever encountered :roll:
Fintan is correct, Geoff and Ramkuman - local reference variables DON'T get garbage collected !!! All local variables live on the stack, and the GC doesn't touch the stack, only the heap.
I promise, the exam is going to be very strict on this point. When the exam asks how many 'things' are eligible for GC it only wants to know about objects, not local reference variables, because, as we all know, local reference variables don't get garbage collected
Geoff Peters
Greenhorn

Joined: Dec 15, 2002
Posts: 4
Boys / Girls, pardon my french but this is really p**sing me off - I just wrote the exam for a second time in two weeks and failed again, first time by one mark (got 0 on the garbage collection question), this time by two (with zero on garbage collection again).
Not only do I get different answers here from people, I get different answers in reference materials on the subject, but Sun asks specific questions on the subject!!! How can it be possible to say "well it may happen or it may not based on platform and thread priority" and then ask a specific question on an exam???
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Geoff,
Garbage collection cannot be forced, but hinted via System.gc();
The correct questions (the ones are asked in the exam) about gc are worded like this:
At what line the object created in line X is earlier made eligible for garbage collection?
(Well, they might use a better English after all )
Notice:
a) the question is about an object. Only objects, not variables, are garbage collected.
b) the question is about eligibility of an object to the garbage collector regardles whether it happens to run or not. Because this cannot be forced they will not be asking if a an object was in fact garbage collected.
c) pay attention to alias for an object, a variable pointing the same object as other variable does. As long as one of them still keeps a reference to the object, this is not eligible for garbage collection.
d) it is not true that an object is eligible for garbage collection when there are not references pointing to it left. Some of the references could be from objects that are also eligible for garbage collection. Those objects form a cycle of references to each other, but given that no other object is referencing the objects in the cycle from outside of it, the whole cycle of objects is eligible for garbage collection.
Don not doubt to question again.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection question