wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes still a GC question ,help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "still a GC question ,help" Watch "still a GC question ,help" New topic
Author

still a GC question ,help

andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
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.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
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.


Amir
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
so the answer is never, infact.
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
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.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
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.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Two good articles on this subject:
Reference Objects and Garbage Collection
Inside the Java 2 Virtual Machine - 9. Garbage Collection


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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 ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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.
 
 
subject: still a GC question ,help