• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

GC

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
programmer has written the following class to prevent garbage collection of the objects of this class. Is he mistaken?



and the answer is TRUE.
but i m unable to underttand why its ture. somebody please explain how can we prevent garbage collection.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ushma, please quote your sources.

Objects become a candidate for garbage collection when there is no live thread anymore that references the object directly or indirectly. Simply making the object refer to itself by means of a member variable doesn't help to make this true. So this does not prevent garbage collection.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the only way to ensure it is safe from the gc is to make the "a" field static and public, or to make it private but accesible with a public static method.

That's what I understood about the singleton pattern
(http://en.wikipedia.org/wiki/Singleton_pattern)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The singleton pattern brings in several other issues which really aren't relevant here. As far as garbage collection is concerned, it doesn't really matter whether a field is public or private. And making the field static - well, that can prevent one object from being collected. However if you create a second object of the same clas, then "a = this" will be executed a second time, and since a is static the reference to the previous "this" object will be lost. So making a static will prevent the most recently created object from being collected, but it won't prevent them all from being collected.

If you really wanted to prevent collection of all objects in a class (and it's a mystery why anyone would ever want to do this), you could do something like this:

Here the collection ensures that all Uncollectable objects will not be collected, because it retains a reference to each one.

(Technically these can still be collected if the class itself is unloaded, which requires that you load the class in the first place using a special ClassLoader, and then make that loader available for collection. That's far outside the scope of what you need for the exam however.)
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic