• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Finalization and Cleanup

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
This is a program from Thinking in Java, Bruce Eckel.The author says "If any portions of the object are not properly cleaned up, then you have a bug in your program that could be very difficult to find. The value of finalize( ) is that it can be used to discover this condition, even if it isn�t always called." Here, he is using finalize to check the state of instance member checkedOut .If its true
he finds it as an erroneous condition,and prints out a message regarding it.And since second object(see line# 1) ,never calls checkIn(), and therefore checkedOut never necomes false ,for this instance.Now my questions is:
1)Firstly, am I correct with my understanding of the program?
2)if yes,then,whether setting checkedOut to false, which is the default value for boolean instance variables, makes the object properly cleaned up,and thus an ideal situation for the garbage collector to work.Also consider this explanation of the author
"Note that System.gc( ) is used to force finalization (and you should do
this during program development to speed debugging). But even if it isn�t,it�s highly probable that the errant Book will eventually be discovered
through repeated executions of the program (assuming the program
allocates enough storage to cause the garbage collector to execute).
"


please help me.
THANKS
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helo!
I do not think that cant help you on this but I have tried to raise questions on finalize on http://www.javaranch.com/ubb/Forum24/HTML/011374.html and I would like to see somebody to come up and explain this.
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Antti,
I don't understand what you are saying,please be more clear .
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What part?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This part:
"I do not think that cant help you on this ...."
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Percy is right.
Thanks Percy
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Percy Densmore:
This part:
"I do not think that cant help you on this ...."


Yup, seems odd.

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In regards to the original code snippet, I find your interpretation in point 1 correct as far as my understanding of the system goes.


The author seems to be using the finalize method for an internal post-condition check of sorts as I see it. He's looking to see if he "lost" any books while they were checked out. It's interesting, but given the stages in finalize in the VMs, I'm not sure that the System.gc() will <em>always</em> force the finalize method on objects to be called. It's not a guarantee. The spec actually reads "best effort". There is also, of note, the method System.runFinalization() which will run all objects in the state of cleanup whose finalizers have not executed.


I'm not a VM guru to speak on the reliability of this method, but it seems to me it would work, but may or may not be definitive according to the specification. Perhaps someone closer to the problems inherent in the various VMs could comment?


Just recalling my latest Java University session with Peter Haggar who pointed me to this board in the first place, and his session on <em>Advanced Java Programming Language Techniques</em> states in Chapter 6 --


"... Do NOT rely on finalization() methods at all

- They are not guaranteed to execute

- Objects are not guaranteed to be freed


I'd call Peter a definite authority on the subject. His book
    Practical Java is on my list of "to buy" and likely deals with the subject in some depth as well.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dallas.
It was a great help to me,on this weird topic.
Regards.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bindesh Vijayan:
Thanks Dallas.
It was a great help to me,on this weird topic.
Regards.


Somehow I hear 'em bells ringing...

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic