• 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

finalize method

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes even than finalize method is called only once
 
praneeth kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the reply.

Is there any way to check this practically?

Thank You.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont have the code but their are refrences(phantom refrence etc) which can be obtained before the object is GC for some proccessing need to be done before the object is destroyed you can try this.

And this is the reason finalize is not called twice it'll turn up into an infinite loop.

object destroy -- finalize call --- obtain refrence----do something --- destroy object -- finalize call --- so on.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


praneeth kumar

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?




if you invoke finalize method explicitly on any object, finalize will be invoked then also on that object by JVM. But JVM invokse only once on that object. If you try to resurrect that object through finalize, and when again that object becomes elligible for garbage collection, this time JVM wont invoke finalize() on it.

here is a sample code::



somehow System.gc() did perform garbage collection (atleast on my system) and the O/P I got::

E:\Priyam>java Test
102010

Though I have not resurrected the object, but you can try your self...

[ July 23, 2007: Message edited by: Priyam Srivastava ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

Praneeth asked:

Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?



No it is different.
And yes, the finalizer daemon may invoke it again.

The GC will call finalize only once (Maximum. Maybe never.). How many times you invoked finalize directly does not matter.
Try to design some example code to explore.



Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic