• 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

concept about finalize

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can you tell me what's the difference between using finalize() directly like anObject.finalize(); and using System.runFinalization();
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling finalize directly is just like calling any method and will occur synchronously.
Although its probably a bad idea to call finalize directly as thats not what its there for. finalize is used to cleanup and release resources when a instance is released. finalize will get called at some point between when the last reference releases the instance and when the instance is garbage collected.
System.runFinalization() provides a hint to the JVM that it should run any outstanding finalize methods for the released instances awaiting garbage collection. Note there is no way to know which instances finalize will be called or if in fact any are. This is analigous (big word dodgy spelling) to calling System.gc
Look in the JLS section 12.6 for further information.
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Phil Sharp:
Calling finalize directly is just like calling any method and will occur synchronously.


Hi,Phil
Thanx for explanation.But why you say calling finalize directly will make it occur "synchronously"? the signature of finalize() is protected void finalize throws Throwable() --no synchronized modifier.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard,
Usually, the JVM is responsible for invoking that method when an object is to be garbage collected so that you have an opportunity to tidy things up in your instance.
Make the difference between the synchronized keyword and synchronous which is a term for saying that a method will not be invoked until the previous one has finished executing.
For instance, the start method of class Thread is not synchronous, because it returns immediately and let the Thread scheduler do its job, that is when you start a thread you get back the control almost directly (this is an asynchronous invocation).
In clear, if you decide to invoke finalize by yourself, the method will behave exactly as another method, a new invocation frame will be created, the method's body will be executed and the method will return. The method invocation following the invocation to finalize will only begin executing when finalize has finished (this is synchronous invocation).
HIH
[ February 12, 2002: Message edited by: Valentin Crettaz ]
 
Phil Sharp
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard
Sorry for the confusion around synchronous. Valentin has explained my meaning far better than I could.
Hopefully everything is clear now.
Phil
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic