aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when will it be Garbage collected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when will it be Garbage collected?" Watch "when will it be Garbage collected?" New topic
Author

when will it be Garbage collected?

Abhishek Sharma
Ranch Hand

Joined: Jan 09, 2006
Posts: 68
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

Joined: Oct 15, 2005
Posts: 72
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

Joined: Jan 09, 2006
Posts: 68
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

Joined: Mar 14, 2006
Posts: 33
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 15, 2005
Posts: 72
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

Joined: Nov 07, 2005
Posts: 36
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


SCJP 5<br />SCWCD 5
Kotto Bass
Greenhorn

Joined: Jan 27, 2006
Posts: 17
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

Joined: Mar 16, 2004
Posts: 317
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


CIAO Peter M. Cooke
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
I stand corrected there is no way to know!
 
 
subject: when will it be Garbage collected?