This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when will it be Garbage collected?

 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,


void start()
{
A a = new A();
B b = new B();
a.s(b);
b = null; //line1
a = null; //line2
System.out.println("Start Complete"); //line3
}

When is the B object, created in line 14, eligible for Garbage collection.
a) After line 1
b) After line 2
c) After line 3(When method ends)
d) There is no way to be absolutely certain.

What will be correct answer of it...
I think "c" is most suitable...
Plz give suggestions.
 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I know, an object can be garbage collected by the system when there are no references to it, so when you set b = null that should be it.

What is more important it will not be garbage collected, it will become available for garbage collection. You can never know when one object will be collected.

If i am wrong someone please correct me
 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Branko,

Its true that when we set b = null
it will be available for Garbage Collection but before doing that we r sending the reference variable as a parameter in function s
___________________
a.s(b);
___________________
it means that now that object is referenced by local variable of method "s"
and thus it depends on the method so according to me the correct answer should be "d".
I m sorry in my last post i wrote it as "c" it was just a mistake.
Plz feel free to give ur suggestions.

Abhishek
 
mithun gooty
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello abishek,
i feel, the reference variable b sent as the parameter will have a allias in the method body... so it does not come into picture when b is set to null.. i feel it is eligible for garbage collection when it is set to null...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible that there could be another reference variable in the method which is set to point to the object.
 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still going with a)
Why? If we reference b somewhere and set b to null than the reference of b will point to null also. Am I on the right track?
[ March 14, 2006: Message edited by: Branko Santo ]
 
Alex Khvatov
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the answer is d. There is no way to be absolutely sure when b will be GC-ed. We do not know if object a has any members of type b and that s is a 'setter' for that class member. if it is the case then b will be GC after 'a=null'. But if method s is not a 'setter', then b would be eligible for GC after b=null line.
So again, my guess is (d).
Alex
 
Kotto Bass
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is b. Eventhough the reference is passed to a method, when that method returns, any local variable referencing the object will no longer refer to it (local scope). So, after line 1, the object should be available for garbage collection.
[ March 14, 2006: Message edited by: Kotto Bass ]
 
peter cooke
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishekxp sh:
Hello Ranchers,


void start()
{
...
a.s(b);
b = null; //line1
a = null; //line2
.....
.



We all know that B cannot be garbage collected until at least b is set to null.

As for dealing with the a.s(b) ---- a copy of the b reference is passed to a.s(). depending on what s() does a referece can be kept around for a long time. S could spin off a low priorty thread that has an endless loop that process B. also s() could set a static reference to "B" and then "B" will never be GC'ed
 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected there is no way to know!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic