This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Kefe Abalov
Greenhorn

Joined: Jul 15, 2003
Posts: 4
given:
12.void start(){
13.A a = new a();
14.B b = new b();
15.a.s(b);
16.b=null;
17.a=null;
18.System.out.println("start is over");
}
when the object B ,created in the line 14,eligible for garbage collection?
A.after the line 16.
B.after the line 17.
C.after the line 18
D.none of above
answer:C
who can tell me why?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
An object is eligible for GC when is not reachable by any existing thread.
If I am not mistaken, the method a.s(b) receives as a parameter b, but we don't know what's gonna happen with that reference inside that method. It can be assigned to another reference, etc. So even if we execute line 16, we don't know for sure what really happened inside method s(). It is only eligible when the method is finished, which is out of scope.
Please correct me if I'm wrong
[ July 20, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alex Radomski
Greenhorn

Joined: Apr 24, 2003
Posts: 24
I wonder if there's missing code in this question?
I think Andres you're correct.
We don't know for certain what will happen to the b reference once s() method is called.
It might be reassigned to another variable of B type?
Alex
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
If the functioning of the a.s(b) is not provided then answer c can not be considered correct. I guess its a wrong question or you can a correct question with wrong answers.
[ July 20, 2003: Message edited by: Anupam Sinha ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
kef,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


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

Joined: Jun 02, 2003
Posts: 76
correct me if i am wrong....
No matter what happens in the method a.s(b), once b=null doesn't it mean all the variables pointing to this reference becomes null and so shouldn't the correct answer be 'after line 16'???
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
hello,
no matter what happens to b inside the method s(). but as long as object a is pointing to objcet b thru s(), only after a is set to null b is free for GC.
If Wrong...plz correct me!
JC
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi all
Well the reason for my earlier answer that it is not possible to know for sure whether b is elligible for GC or not is that the method a.s() may store the reference of b in some varibale. Consider this code for a.s()

Now in here, the object reference contained in o(b in the orignal code) is being transfered to a method in AnotherClass. This will prevent the varibale b from being GCed. Remember object references are GCed and not variables.
[ July 22, 2003: Message edited by: Anupam Sinha ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collection