wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A GC Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A GC Question" Watch "A GC Question" New topic
Author

A GC Question

Jiaxu Gu
Greenhorn

Joined: Oct 29, 2005
Posts: 12
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

Joined: Sep 26, 2005
Posts: 67
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

Joined: Oct 27, 2005
Posts: 68
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

Joined: Sep 27, 2005
Posts: 73
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 ]

SCJP- 98%<br />SCWCD-92%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Sep 27, 2005
Posts: 73
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

Joined: Nov 02, 2005
Posts: 99
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.


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
david lightman
Ranch Hand

Joined: Nov 03, 2004
Posts: 82
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

Joined: Nov 02, 2005
Posts: 99
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.
 
wood burning stoves
 
subject: A GC Question