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.
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
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?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
0
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
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: 32833
4
posted
0
I take your point; at least a static List is guaranteed still to exist.
Achint Verma
Greenhorn
Joined: Jul 13, 2012
Posts: 11
posted
0
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: 32833
4
posted
0
In that case I shall fill in the Foo class and let you run it. Execute the main method several times.