Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > finalize() not guaranteed to be called -- ever

Reply
Thread Tools

finalize() not guaranteed to be called -- ever

 
 
Paul J. Lucas
Guest
Posts: n/a
 
      08-23-2005
According to:

http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm

that says in part:

The Java interpreter can exit without garbage collecting
all outstanding objects, so some finalizers may never be
invoked.

Is this still true in Java 1.4 and later?

- Paul
 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      08-23-2005
Hi,

Paul J. Lucas wrote:
> According to:
>
> http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm
>
> that says in part:
>
> The Java interpreter can exit without garbage collecting
> all outstanding objects, so some finalizers may never be
> invoked.
>
> Is this still true in Java 1.4 and later?


I don't know, but in newer Versions there is a Shutdown-Hook-mechanism
for dealing with that.

Ciao,
Ingo

 
Reply With Quote
 
 
 
 
Paul J. Lucas
Guest
Posts: n/a
 
      08-23-2005
Ingo R. Homann <(E-Mail Removed)> wrote:
>
> Paul J. Lucas wrote:
> > According to:
> >
> > http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm
> >
> > that says in part:
> >
> > The Java interpreter can exit without garbage collecting
> > all outstanding objects, so some finalizers may never be
> > invoked.
> >
> > Is this still true in Java 1.4 and later?

>
> I don't know, but in newer Versions there is a Shutdown-Hook-mechanism
> for dealing with that.


I know, and that's not what I'm asking about. Why should *I*
have to add a shutdown hook to call System.runFinalizers() ? I
want to know if the JVM will *guarantee* that all finalizers
will *automatically* be executed prior to JVM shutdown

- Paul
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-23-2005


Paul J. Lucas wrote:
> Ingo R. Homann <(E-Mail Removed)> wrote:
>>
>>I don't know, but in newer Versions there is a Shutdown-Hook-mechanism
>>for dealing with that.

>
>
> I know, and that's not what I'm asking about. Why should *I*
> have to add a shutdown hook to call System.runFinalizers() ? I
> want to know if the JVM will *guarantee* that all finalizers
> will *automatically* be executed prior to JVM shutdown


Ingo seems to be saying something slightly different:
not that you should use a shutdown hook to run finalizers,
but that you should use it for the cleanup you're now (it
seems) trying to do in the finalizers.

FWIW, the gurus (I am not one) seem to be unanimous in
warning against using finalizers as destructors. Eckel puts
it this way: Garbage collection is about memory, finalization
is an aspect of garbage collection, therefore finalization is
about memory and anything that isn't about memory is out of
place in a finalizer. Bloch writes at length about the dangers
of finalization, and recounts the tale of an OutOfMemoryError
that was caused by relying on finalizers -- not by bug-ridden
finalizers, mind you, but simply by reliance on them. Add to
this some of the scarier passages where the Javadoc explains
why System.runFinalizersOnExit() is deprecated (sounds like a
hint, doesn't it?), and I think there are enough bad omens to
make you consider other avenues ...

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      08-23-2005

"Paul J. Lucas" <(E-Mail Removed)>
wrote in message
news:zpHOe.3801$(E-Mail Removed) m...
> According to:
>
> http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm
>
> that says in part:
>
> The Java interpreter can exit without garbage collecting
> all outstanding objects, so some finalizers may never be
> invoked.
>
> Is this still true in Java 1.4 and later?


My book says that if you execute "System.exit(0);" that it does
an exit
without doing anything else.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-23-2005
Joan wrote:
>
> "Paul J. Lucas" <(E-Mail Removed)> wrote in
> message news:zpHOe.3801$(E-Mail Removed) m...
>
>> According to:
>>
>> http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm
>>
>> that says in part:
>>
>> The Java interpreter can exit without garbage collecting
>> all outstanding objects, so some finalizers may never be
>> invoked.
>>
>> Is this still true in Java 1.4 and later?

>
>
> My book says that if you execute "System.exit(0);" that it does an exit
> without doing anything else.


Books often skip details and advanced information to keep down the size
and focus on the basics. Once you have the basic concepts, use the API
Javadocs and Java Language Specification to fill in missing details.

The System.exit documentation indicates that System.exit(n) is
effectively equivalent to Runtime.getRuntime().exit(n).

The documentation for exit in Runtime says:

"The virtual machine's shutdown sequence consists of two phases. In the
first phase all registered shutdown hooks, if any, are started in some
unspecified order and allowed to run concurrently until they finish. In
the second phase all uninvoked finalizers are run if
finalization-on-exit has been enabled. Once this is done the virtual
machine halts."

It is also described as throwing "SecurityException - If a security
manager is present and its checkExit method does not permit exiting with
the specified status" so the exit is conditional.

This is the same as "exit without doing anything else" in many cases,
but not all. This type of simplification is typical of many books, and
harmless as long as readers understand that the book is not telling the
whole story, and know where to look for more data.

Patricia
 
Reply With Quote
 
Doug Pardee
Guest
Posts: n/a
 
      08-23-2005
> I want to know if the JVM will *guarantee* that all finalizers
> will *automatically* be executed prior to JVM shutdown


No, it won't guarantee that.

 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      08-24-2005


Paul J. Lucas wrote:
> According to:
>
> http://www.unix.org.ua/orelly/java-ent/jnut/ch03_03.htm
>
> that says in part:
>
> The Java interpreter can exit without garbage collecting
> all outstanding objects, so some finalizers may never be
> invoked.
>
> Is this still true in Java 1.4 and later?


Yes. Well, not completely. If you look at:

http://java.sun.com/docs/books/vmspe...doc.html#19152

you will see:

A program can specify that all finalizers that have not been
automatically invoked are to be run before the virtual machine exits.
This is done by invoking the method runFinalizersOnExit of the class
System with the argument true.4 By default finalizers are not run on
exit. Once running finalizers on exit has been enabled it may be
disabled by invoking runFinalizersOnExit with the argument false. An
invocation of the runFinalizersOnExit method is permitted only if the
caller is allowed to exit and is otherwise rejected by the security manager.
 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      08-24-2005
Hi,

Eric Sosman wrote:
>>Ingo R. Homann <(E-Mail Removed)> wrote:
>>
>>>I don't know, but in newer Versions there is a Shutdown-Hook-mechanism
>>>for dealing with that.

>>
>>
>> I know, and that's not what I'm asking about. Why should *I*
>> have to add a shutdown hook to call System.runFinalizers() ? I
>> want to know if the JVM will *guarantee* that all finalizers
>> will *automatically* be executed prior to JVM shutdown

>
>
> Ingo seems to be saying something slightly different:
> not that you should use a shutdown hook to run finalizers,
> but that you should use it for the cleanup you're now (it
> seems) trying to do in the finalizers.
>
> FWIW, the gurus (I am not one) seem to be unanimous in
> warning against using finalizers as destructors...


Well, in my experience, finalizers work very well if you only use them
for cleaning up and not doing something dirty like the following:

class X {
static X x;
void finalize() {
x=this;
}
}

Anyhow, although finalizers are working perfectly in practise, It is
correct that theoretically you cant rely on them.
So, I would not rely on them when programming a control application for
a nuclear power plant.

Ciao,
Ingo

 
Reply With Quote
 
Paul J. Lucas
Guest
Posts: n/a
 
      08-24-2005
Doug Pardee <(E-Mail Removed)> wrote:
> > I want to know if the JVM will *guarantee* that all finalizers
> > will *automatically* be executed prior to JVM shutdown

>
> No, it won't guarantee that.


Then what's the point of a finalizer? The JVM *should*
guarantee that just like C++ guarantees that all destructors
for all locally-scoped and static objects *will* be called
prior to program termination (unless you do something drastic
like call abort()).

- Paul
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is called carry chain structure in FPGA is called in IC? Weng Tianxiang VHDL 6 09-12-2007 07:19 AM
Can a static function defined in a C file be ever referred (called) externally ? jeniffer C Programming 12 03-29-2006 03:39 PM
C++ static destructor guaranteed to be called? Paul J. Lucas Java 21 10-22-2005 02:36 AM
don't you ever, don't you ever, lower yourself, forgetting all your standards Jason ASP .Net Web Controls 0 07-06-2004 12:50 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments