wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes concept about finalize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "concept about finalize" Watch "concept about finalize" New topic
Author

concept about finalize

Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
Hi,
can you tell me what's the difference between using finalize() directly like anObject.finalize(); and using System.runFinalization();


Richard Wilson
Phil Sharp
Ranch Hand

Joined: Nov 08, 2001
Posts: 40
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

Joined: Jan 12, 2002
Posts: 57
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.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Phil Sharp
Ranch Hand

Joined: Nov 08, 2001
Posts: 40
Richard
Sorry for the confusion around synchronous. Valentin has explained my meaning far better than I could.
Hopefully everything is clear now.
Phil
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: concept about finalize