Forums Register Login

still a GC question ,help

+Pie Number of slices to send: Send
Given:
public class andy {
public object amy () {
object o = new float (3.1415F);
object [] oa = new object[1];
oa[0]= o;
o = null;
return oa[0];
}
}
When is the float object created in line 3, eligible for garbage collection?
A. Just after line 5
B. Just after line 6
C. Just after line 7 (that is, as the method returns)
D. Never in this method
i think c is the right answer,because althought the oa[0] is returned by the method ,but there is no variable to receive or hold the object reference ,so when the method returns the object is eligible for GC.
+Pie Number of slices to send: Send
after the assignment, oa[0]= o; you copy the reference to oa[0] which is still pointing to the float object created at line 3.
+Pie Number of slices to send: Send
so the answer is never, infact.
+Pie Number of slices to send: Send
but how do you know whether there is a variable to receive the returned oa[0]?
assume there is no variable to do this,then the object must be GC.
+Pie Number of slices to send: Send
An object becomes eligible for GC when it cannot be referenced by any active part of the program.
Here you don't know if there is a variable outside of the method that can receive the returned value oa[0]. if there isn't, then the variable becomes eligible for GC after the method ends. However, in this particular example, you cannot assume that there is no variable to recieve the returned value, since the full source code is not shown. therefore, it's better to say never eligible for GC.
+Pie Number of slices to send: Send
Two good articles on this subject:
Reference Objects and Garbage Collection
Inside the Java 2 Virtual Machine - 9. Garbage Collection
+Pie Number of slices to send: Send
here's some info from Mike Meyers' Certification Passport - Java 2 exam study book (pages125-126):
quote:
"In the following code, a reference to the object
oref, created in the method createObject() is passed to main(). The object oref will not become eligible for garbage collection when the method createObject() completes because a reference to the object still exists in the main() method."

when objects are created in methods, the object becomes eligible for GC when the method execution ends as long as no other references to the object exists.
re: the question that was submitted - where did that question come from ? it seems to be a very poorly designed question.
[ July 04, 2002: Message edited by: david eberhardt ]
+Pie Number of slices to send: Send
 

Originally posted by andy lau:
but how do you know whether there is a variable to receive the returned oa[0]?
assume there is no variable to do this,then the object must be GC.


I assumed that the question tests for the knowledge that another reference to the object was created in that method and that setting the original reference to null did not make the object eligible for GC. As long as any other references exist, the object lives on ... of course the reference must be passed back outside the method also.
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 719 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:34:56.