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

A GC Question

 
Jiaxu Gu
Greenhorn
Posts: 12
  • 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 completed");
19. }
When is the B object, created in line 14, eligible for garbage collection?
A. After line 16.
B. After line 17.
C. After line 18 (when the method ends).
D. There is no way to be absolutely certain.
E. The object is NOT eligible for garbage collection.
Answer: C
Please explain the answer,is it wrong?
 
Abhijit Sontakey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The answer is definetly wrong. Object of class b becomes eligible for garbage collection after it is set to null at 16. As the object is not referenced by any active part of thr program, it becomes eligible for garbage collection.
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is after line 17, since before line17, object B is still referenced by object A...

Many be I am wrong, anyone else?

Kev
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kevin Lam:
I think the answer is after line 17, since before line17, object B is still referenced by object A...

Many be I am wrong, anyone else?

Kev



Yes Kevin, u r wrong.
Obj 'a' is not referring object 'b', just 'b'is passed to the method of 'A'.
B'coz "b=null" statement executes after finishing the execution of
"a.s(b)" method, we need not bother abt the method body..right?

When u say "b=null" there is no chance of having any reference to 'b'.
So ans is option-A



[ November 12, 2005: Message edited by: Mahendar Reddy ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is D, simply because you do not know what's inside a.s(B b). If it would create another reference to the object referenced by b (like a member field of the instance start() was invoked on), then it could not be garbage collected yet. This reference need not be part of either the object referenced by a or b.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhijit Sontakey:
As the object is not referenced by any active part of thr program
Well it might be as a result of the call to a.s(). Without its body you simply cannot tell.
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya Rob may be right...
We may assign 'b' to some static var in the method, there 'b' lives as
long as class lives.

 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Rob. We can't certain when Object B will be GC-ed, since we don't how the method 's' will deal with b reference.
Assume that if a thread has been created inside method s and the reference b being passed to it. In this case, eventhough the control returns from the method 's', Object B will be still referenced by the Thread.
 
david lightman
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what is tough for guys like me,

the first guys says he thinks its answer C, the second says no its A, then the third says no, its B, then another chimes in and says he thinks its A too, then the next guy says you are all wrong, its D...and on and on...

what is the answer....?
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope it will be clear after executing the below pgm.



In the above pgm if you uncomment simThread.theSecond = b; statement, then theSecond object wont be garbage collected.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic