Is there a destructor for Java?

前端 未结 22 1382
[愿得一人]
[愿得一人] 2020-11-22 11:47

Is there a destructor for Java? I don\'t seem to be able to find any documentation on this. If there isn\'t, how can I achieve the same effect?

To make my question m

相关标签:
22条回答
  • 2020-11-22 12:28

    There is a @Cleanup annotation in Lombok that mostly resembles C++ destructors:

    @Cleanup
    ResourceClass resource = new ResourceClass();
    

    When processing it (at compilation time), Lombok inserts appropriate try-finally block so that resource.close() is invoked, when execution leaves the scope of the variable. You can also specify explicitly another method for releasing the resource, e.g. resource.dispose():

    @Cleanup("dispose")
    ResourceClass resource = new ResourceClass();
    
    0 讨论(0)
  • 2020-11-22 12:29

    First, note that since Java is garbage-collected, it is rare to need to do anything about object destruction. Firstly because you don't usually have any managed resources to free, and secondly because you can't predict when or if it will happen, so it's inappropriate for things that you need to occur "as soon as nobody is using my object any more".

    You can be notified after an object has been destroyed using java.lang.ref.PhantomReference (actually, saying it has been destroyed may be slightly inaccurate, but if a phantom reference to it is queued then it's no longer recoverable, which usually amounts to the same thing). A common use is:

    • Separate out the resource(s) in your class that need to be destructed into another helper object (note that if all you're doing is closing a connection, which is a common case, you don't need to write a new class: the connection to be closed would be the "helper object" in that case).
    • When you create your main object, create also a PhantomReference to it. Either have this refer to the new helper object, or set up a map from PhantomReference objects to their corresponding helper objects.
    • After the main object is collected, the PhantomReference is queued (or rather it may be queued - like finalizers there is no guarantee it ever will be, for example if the VM exits then it won't wait). Make sure you're processing its queue (either in a special thread or from time to time). Because of the hard reference to the helper object, the helper object has not yet been collected. So do whatever cleanup you like on the helper object, then discard the PhantomReference and the helper will eventually be collected too.

    There is also finalize(), which looks like a destructor but doesn't behave like one. It's usually not a good option.

    0 讨论(0)
  • 2020-11-22 12:29

    The closest equivalent to a destructor in Java is the finalize() method. The big difference to a traditional destructor is that you can't be sure when it'll be called, since that's the responsibility of the garbage collector. I'd strongly recommend carefully reading up on this before using it, since your typical RAIA patterns for file handles and so on won't work reliably with finalize().

    0 讨论(0)
  • 2020-11-22 12:31

    I agree with most of the answers.

    You should not depend fully on either finalize or ShutdownHook

    finalize

    1. The JVM does not guarantee when this finalize() method will be invoked.

    2. finalize() gets called only once by GC thread. If an object revives itself from finalizing method, then finalize will not be called again.

    3. In your application, you may have some live objects, on which garbage collection is never invoked.

    4. Any Exception that is thrown by the finalizing method is ignored by the GC thread

    5. System.runFinalization(true) and Runtime.getRuntime().runFinalization(true) methods increase the probability of invoking finalize() method but now these two methods have been deprecated. These methods are very dangerous due to lack of thread safety and possible deadlock creation.

    shutdownHooks

    public void addShutdownHook(Thread hook)
    

    Registers a new virtual-machine shutdown hook.

    The Java virtual machine shuts down in response to two kinds of events:

    1. The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or

    2. The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

    3. A shutdown hook is simply an initialized but non-started thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled.

    4. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if the shutdown was initiated by invoking the exit method.

    5. Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit.

      But even Oracle documentation quoted that

    In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly

    This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

    Conclusion : use try{} catch{} finally{} blocks appropriately and release critical resources in finally(} block. During release of resources in finally{} block, catch Exception and Throwable.

    0 讨论(0)
  • 2020-11-22 12:32

    No, java.lang.Object#finalize is the closest you can get.

    However, when (and if) it is called, is not guaranteed.
    See: java.lang.Runtime#runFinalizersOnExit(boolean)

    0 讨论(0)
  • 2020-11-22 12:34

    Nope, no destructors here. The reason is that all Java objects are heap allocated and garbage collected. Without explicit deallocation (i.e. C++'s delete operator) there is no sensible way to implement real destructors.

    Java does support finalizers, but they are meant to be used only as a safeguard for objects holding a handle to native resources like sockets, file handles, window handles, etc. When the garbage collector collects an object without a finalizer it simply marks the memory region as free and that's it. When the object has a finalizer, it's first copied into a temporary location (remember, we're garbage collecting here), then it's enqueued into a waiting-to-be-finalized queue and then a Finalizer thread polls the queue with very low priority and runs the finalizer.

    When the application exits, the JVM stops without waiting for the pending objects to be finalized, so there practically no guarantees that your finalizers will ever run.

    0 讨论(0)
提交回复
热议问题