Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

garbage collection

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are referencing to a new object the old object will be eligible for GC.

Unless the old object is static i guess ....?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic