• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collector

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I want to check for a condition and prevent it to be gced if the condition is not met how do I write it? How should my finalize method make sure of it not happening?
Thanks,
Shails
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in general, the only objects that get GCed are ones with no references (reachable by a live thread). So it's easy to keep objects around, just keep a reference to them.
I wonder why you want to do this?
 
shailu kneni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading Bruce Eckel's book where he gives an example - there is this book class, objects of which can not be GCed if they are checked out. So he has this condition in the finalize method where he checks the value of the checkedout boolean field. If it is true then the object can not be taken out of memory. The book class has a constructor which takes the boolean. He cteates a book object with the boolean as true. So if this object is about to be GCed it is an error so in the finalize method he addds this check. My questions is based on the check how do you make sure the object is not GCed. If the book is really checked out there will be a reference to it so the finalize method will never be called. But if it is attempted to be taken out of memory by mistake then this works as a check.

Thanks,
Shails
[ February 16, 2004: Message edited by: shailu kneni ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that makes a little sense, kind of...
A couple of ideas though... First off, the GC is unpredictable, hence the calling of finalize() is unpredictable. We pretty much recommend that you not override the finalize method, (what important activity do you put in a method that is never guaranteed to run?).
Is this use of finalize meant to be a teaching aide? Or a debugging aide? I suppose that finalize could check the flag, and if the object is not meant to be GCed, then finalize could send a reference to a live object, thus resurrecting this object. But that seems on the surface like a weird way to catch a logic flaw in some other part of your program...
 
Destroy anything that stands in your way. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic