File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic

garbage collection

Kefe Abalov

Joined: Jul 15, 2003
Posts: 4
12.void start(){
13.A a = new a();
14.B b = new b();
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
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

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?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
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

Joined: Aug 26, 2001
Posts: 7610
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!

[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
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!
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
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 ]
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!