• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but what's the final answer?
i think 'cause the return sentence,so it 'll cause gc after the return. is that right?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Geoff Peters
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obj = null ???
are we looking at the same code?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic