aspose file tools*
The moose likes Beginning Java and the fly likes Uneligibilizing an object for Garbage Collection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Uneligibilizing an object for Garbage Collection." Watch "Uneligibilizing an object for Garbage Collection." New topic
Author

Uneligibilizing an object for Garbage Collection.

Achint Verma
Greenhorn

Joined: Jul 13, 2012
Posts: 12
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Campbell, did you mean to make the List a static member?


luck, db
There are no new questions, but there may be new answers.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8010
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I take your point; at least a static List is guaranteed still to exist.
Achint Verma
Greenhorn

Joined: Jul 13, 2012
Posts: 12

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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
In that case I shall fill in the Foo class and let you run it. Execute the main method several times.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uneligibilizing an object for Garbage Collection.