Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

still a GC question ,help

 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the answer is never, infact.
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two good articles on this subject:
Reference Objects and Garbage Collection
Inside the Java 2 Virtual Machine - 9. Garbage Collection
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic