finalize() not guaranteed to be called -- ever

Discussion in 'Java' started by Paul J. Lucas, Aug 23, 2005.

  1. Paul J. Lucas, Aug 23, 2005
    #1
    1. Advertising

  2. 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
     
    Ingo R. Homann, Aug 23, 2005
    #2
    1. Advertising

  3. Ingo R. Homann <> 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
     
    Paul J. Lucas, Aug 23, 2005
    #3
  4. Paul J. Lucas

    Eric Sosman Guest

    Paul J. Lucas wrote:
    > Ingo R. Homann <> 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 ...

    --
     
    Eric Sosman, Aug 23, 2005
    #4
  5. Paul J. Lucas

    Joan Guest

    "Paul J. Lucas" <>
    wrote in message
    news:zpHOe.3801$...
    > 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.
     
    Joan, Aug 23, 2005
    #5
  6. Joan wrote:
    >
    > "Paul J. Lucas" <> wrote in
    > message news:zpHOe.3801$...
    >
    >> 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
     
    Patricia Shanahan, Aug 23, 2005
    #6
  7. Paul J. Lucas

    Doug Pardee Guest

    > 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.
     
    Doug Pardee, Aug 23, 2005
    #7
  8. 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/vmspec/2nd-edition/html/Concepts.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.
     
    Andrea Desole, Aug 24, 2005
    #8
  9. Hi,

    Eric Sosman wrote:
    >>Ingo R. Homann <> 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
     
    Ingo R. Homann, Aug 24, 2005
    #9
  10. Doug Pardee <> 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
     
    Paul J. Lucas, Aug 24, 2005
    #10
  11. Andrea Desole <> wrote:

    > Yes. Well, not completely. If you look at:
    >
    > http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.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.


    It also says:

    This method is inherently unsafe. It may result in
    finalizers being called on live objects while other
    threads are concurrently manipulating those objects,
    resulting in erratic behavior or deadlock.

    So why don't they just fix runFinalizersOnExit() so it's not
    unsafe? If you first killed off all threads but the one doing
    the shutting down, then it would be impossible to deadlock.
    All the threads have to be killed anyway.

    - Paul
     
    Paul J. Lucas, Aug 24, 2005
    #11
  12. Paul J. Lucas wrote:
    >
    > It also says:
    >
    > This method is inherently unsafe. It may result in
    > finalizers being called on live objects while other
    > threads are concurrently manipulating those objects,
    > resulting in erratic behavior or deadlock.


    yes, that's in the Javadoc

    >
    > So why don't they just fix runFinalizersOnExit() so it's not
    > unsafe? If you first killed off all threads but the one doing
    > the shutting down, then it would be impossible to deadlock.


    Yes, it sounds logical; I don't know why it's not different.
    But, if you are just looking for a clean solution to free your resources
    when the application terminates, I think Ingo gave you a good answer.
     
    Andrea Desole, Aug 24, 2005
    #12
  13. Paul J. Lucas wrote:
    > It also says:
    >
    > This method is inherently unsafe. It may result in
    > finalizers being called on live objects while other
    > threads are concurrently manipulating those objects,
    > resulting in erratic behavior or deadlock.
    >
    > So why don't they just fix runFinalizersOnExit() so it's not
    > unsafe? If you first killed off all threads but the one doing


    The word "inherently" in there is a clue.

    > the shutting down, then it would be impossible to deadlock.
    > All the threads have to be killed anyway.


    Two reasons spring to mind:
    o Finalisers can be dependent upon other threads.
    o Threads aren't killed until the plug is pulled on the JVM. If you
    kill a thread that holds locks then you are in trouble. See Thread.kill.

    Any reasonable operating system will clear up process resources when
    they exit. Persistent resource can be cleared up through a shutdown hook.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 24, 2005
    #13
  14. Paul J. Lucas wrote:
    > Doug Pardee <> 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*


    To clear up resources from buggy application code. They can be a useful
    way to free up memory without having a reference queue in some
    circumstances.

    But most finaliser use is down to bad programming.

    > 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()).


    Finalisers are not destructors.

    Java is not C++.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 24, 2005
    #14
  15. Paul J. Lucas

    Eric Sosman Guest

    Thomas Hawtin wrote:
    > Paul J. Lucas wrote:
    >
    >>Doug Pardee <> 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*

    >
    >
    > To clear up resources from buggy application code. They can be a useful
    > way to free up memory without having a reference queue in some
    > circumstances.


    Code needn't be buggy to require finalization. For
    example, a Java object that's backed by a non-Java entity
    via JNI could use finalize() to tell its counterpart it's
    no longer needed.

    > But most finaliser use is down to bad programming.


    That may well be true ;-)

    --
     
    Eric Sosman, Aug 24, 2005
    #15
  16. Paul J. Lucas

    Joan Guest

    <snip>

    > 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.


    As if that is on your job list.
     
    Joan, Aug 24, 2005
    #16
  17. Eric Sosman wrote:
    >
    > Code needn't be buggy to require finalization. For
    > example, a Java object that's backed by a non-Java entity
    > via JNI could use finalize() to tell its counterpart it's
    > no longer needed.


    Then the Java object should have some kind of dispose method.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 24, 2005
    #17
  18. In article <>,
    Joan <> wrote:
    >
    ><snip>
    >
    >> 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.

    >
    >As if that is on your job list.
    >

    Besides, he can't use Java for it anyway.
    --
    "Yo' ideas need to be thinked befo' they are say'd" - Ian Lamb, age 3.5
    http://www.cs.queensu.ca/~dalamb/ qucis->cs to reply (it's a long story...)
     
    David Alex Lamb, Aug 24, 2005
    #18
  19. On 2005-08-23, Patricia Shanahan penned:
    > Joan wrote:
    >>
    >> 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.


    Is this the spot where Thinking in Java is once again mentioned as a
    great book that does actually address this type of information?

    (Disclaimer: I haven't checked to see if this *exact* tidbit is in the
    book)

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Aug 24, 2005
    #19
  20. Thomas Hawtin wrote:
    >
    > Two reasons spring to mind:
    > o Finalisers can be dependent upon other threads.
    > o Threads aren't killed until the plug is pulled on the JVM. If you
    > kill a thread that holds locks then you are in trouble. See Thread.kill.


    but, if you exit the program "cleanly" (that is, without calling
    exit()), shouldn't the finalizers be called after all the threads are
    terminated?
     
    Andrea Desole, Aug 25, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    21
    Views:
    9,637
    Roedy Green
    Oct 22, 2005
  2. Boris
    Replies:
    6
    Views:
    1,396
    Patricia Shanahan
    Apr 21, 2007
  3. Paul Tomblin
    Replies:
    18
    Views:
    523
    Piotr Kobzda
    Apr 26, 2007
  4. andrey
    Replies:
    0
    Views:
    173
    andrey
    Dec 15, 2007
  5. Andrey Nikitin
    Replies:
    1
    Views:
    183
    Nobuyoshi Nakada
    Dec 16, 2007
Loading...

Share This Page