File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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 "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225



when the processing of line 3 begins how many objects of type Q that were
created at line 1 are eligible for garbage collection?

options
0
1
9
10
inderterminate

i thought 10 as there were a total of 10 objects created then why is it giving such a strange output please explain


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
suresh mulagala
Ranch Hand

Joined: Feb 18, 2003
Posts: 41
what does m2 do? im m2 if q1 reference is passed on and not de-referenced anywhere, they all wont be eligible for GC isint it? any other thoughts?
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
ok then the answer is indeterminate, as it depends on method m2 where it can be refereced to null.

But if it is not referenced to null and referenced to any other object then in that situation is it eligible for GC
suresh mulagala
Ranch Hand

Joined: Feb 18, 2003
Posts: 41
If you are referencing to a new object the old object will be eligible for GC.

Unless the old object is static i guess ....?
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by srinivas sridaragaddi:

But if it is not referenced to null and referenced to any other object then in that situation is it eligible for GC


In that case, 9 objects will be eligible for garbage collection as q1 holds on to only one object and that is the last one created in the loop.


apigee, a better way to API!
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by suresh mulagala:

Unless the old object is static i guess ....?


Objects are never static, references are static.
As you said before, if in method m2, some code like this is present:


q3[index++] = q1,

where q3 is either an instance variable(of an instance that is not eligible for gc) or a static variable(if the classloader is not eligible for gc).
index is again a static integer.
In this case, none of the objects will be eligible for gc.

However, if the code is as follows:

q3 = q1

Then 9 objects will be eligible for garbage collection as the above code will only hold on the last object created.
Hope it is clear.
suresh mulagala
Ranch Hand

Joined: Feb 18, 2003
Posts: 41
what if m2 is setting q1 to null ? by the time it gets to the sytem.out, all of them would have been de-referenced and eligible to GC so in that case will not be holding on to any...

Another case: if there were static fields in the class and m2 is setting them to static fields none of them will be eligible to GC as they all have living references.....am i right?
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by suresh mulagala:
what if m2 is setting q1 to null ? by the time it gets to the sytem.out, all of them would have been de-referenced and eligible to GC so in that case will not be holding on to any...

This is the most common mistake i have seen people make.
Even if you set q1 = null inside m2, its a different variable reference you are pointing to null, the variable q1 in m1 method will still point to the last created object.

Originally posted by suresh mulagala:

Another case: if there were static fields in the class and m2 is setting them to static fields none of them will be eligible to GC as they all have living references.....am i right?

This is what i meant when i said there is an array of references (case 1 in my comment). But ofcourse this is a valid case where a class has as many different references as the number of objects created in the loop! People usually use arrays or other data structures for such a scenario.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection