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

Uneligibilizing an object for Garbage Collection.

 
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do we uneligibilize an object for GC using the finalize method ? Kathy and Bates mention "we can write code within the finalize method that passes a reference to the object in question back to another object, effectively uneligiblizing the object". I haven't understood this clearly. Can someone give an example and explain ? Thank you.
 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are all sorts of things you can do. You can stick nails through your hand, or you can use the finalize() method for anything other than cleaning up “native” resources. The latter will hurt more in the long term There you go (in this part of the world pronounced as one word: “thereygo”). You can have an object deleted by the garbage collector and then it becomes a reachable reference again. How’s that for bad programming
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, did you mean to make the List a static member?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Achint Verma wrote:How do we uneligibilize an object for GC using the finalize method ?


Well, "we" don't. And I strongly suggest you don't either.

Kathy and Bates mention "we can write code within the finalize method that passes a reference to the object in question back to another object, effectively uneligiblizing the object".


I can't imagine why you would ever want to do something like that; and frankly, I'm surprised that either Kathy or Bates would suggest it. Are you sure that this isn't in a chapter called "bad programming practise"?

Winston
 
Campbell Ritchie
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Campbell, did you mean to make the List a static member?

I had thought it could be initialised in the //... bit, but it can be static if you so wish. It can’t make that programming any worse that it already is
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I had thought it could be initialised in the //... bit, but it can be static if you so wish. It can’t make that programming any worse that it already is


Not so much the initialisation - but the list would also be eligible for garbage collection otherwise, so it wouldn't necessarily prevent anything (depending on exactly what the GC algorithm is, which I've got little inclination to dig into any further ).
 
Campbell Ritchie
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point; at least a static List is guaranteed still to exist.
 
Achint Verma
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I can't imagine why you would ever want to do something like that; and frankly, I'm surprised that either Kathy or Bates would suggest it. Are you sure that this isn't in a chapter called "bad programming practise"?



@Winston: Those lines are straight out of the book. I definitely agree that it would be a bad programming practice, but I wanted to know as how could we do the stuff they have mentioned.
 
Campbell Ritchie
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case I shall fill in the Foo class and let you run it. Execute the main method several times.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Not so much the initialisation - but the list would also be eligible for garbage collection otherwise


The list might have been passed in via a constructor and so might still have an external reference.
reply
    Bookmark Topic Watch Topic
  • New Topic