File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Kefe Abalov
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Radomski
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic