• 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

Regarding object finalization

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I could not understand some parts(i.e Bold Text) of the A Programmer's Guide to Java SCJP Certification(By Khalid A. Mughal Rolf W. Rasmussen) of Object Finalization it says,

A finalizer can, like any other method, catch and throw exceptions (see Section
6.7, p. 245). However, any exception thrown but not caught by a finalizer that is
called by the garbage collector is ignored, and the finalization of this object is terminated.
The finalizer is only called once on an object, regardless of whether any
exception is thrown during its execution. In case of finalization failure, the object
still remains eligible for disposal at the discretion of the garbage collector (unless
it has been resurrected, as explained in the next subsection). Since there is no
guarantee that the garbage collector will ever run, there is also no guarantee that
the finalizer will ever be called.



What is the reason?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read garbage collection from K & B.
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize method is called by garbage collector. since its no guarantee that garbage collector will ever run therefore there is no guarantee that the finalize method will run too.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the program exits before garbage collector gets a chance to run, so there's no assurance that the finalize method will be called. If an object is eligible for GC, but the program exits before garbage collector runs, then the finalize method won't be called on that object...
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i think it means the finalize method only runs when the garbage collector has started and the object is eligible to get gced....
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:so i think it means the finalize method only runs when the garbage collector has started and the object is eligible to get gced....


finalize method only runs when an object is actually garbage collected, if garbage collector runs and doesn't garbage collect the object, then finalize method doesn't run...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean that if an object is eligible for gc....and if the garbage collector has started....and if the object is going to get gced...the finalize method will run before then object is gced.....

one question....if the garbage collector has started....is it sure that the objects that are eligible for gc will get gced?

 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic