• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A GC Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic