Java有析构函数吗?我好像找不到任何关于这个的文件。如果没有,我怎样才能达到同样的效果?

为了使我的问题更具体,我正在编写一个处理数据的应用程序,规范说应该有一个“重置”按钮,使应用程序恢复到最初的启动状态。但是,除非应用程序关闭或按下重置按钮,否则所有数据必须是“活的”。

作为一个通常的C/ c++程序员,我认为这是微不足道的实现。(因此我打算最后实现它。)我构造了我的程序,使所有“可重置”的对象都在同一个类中,这样当按下重置按钮时,我就可以销毁所有“活动”对象。

我在想,如果我所做的只是解除对数据的引用,并等待垃圾收集器收集它们,如果我的用户重复输入数据并按下重置按钮,是否会出现内存泄漏?我还在想,既然Java作为一种语言相当成熟,应该有一种方法来防止这种情况发生或优雅地解决这个问题。


当前回答

因为Java是一种垃圾收集语言,所以您无法预测对象何时(甚至是否)会被销毁。因此没有直接等价的析构函数。

有一个被称为finalize的继承方法,但它完全由垃圾回收器自行调用。因此,对于需要显式清理的类,约定是定义一个close方法,并仅使用finalize方法进行完整性检查(即,如果close未被调用,则立即执行并记录错误)。

最近有一个问题引发了关于finalize的深入讨论,所以如果需要的话,应该会提供更多的深度……

其他回答

不,这里没有析构函数。原因是所有Java对象都是堆分配和垃圾收集。没有显式的释放(即c++的delete操作符),就没有实现真正析构函数的合理方法。

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.

当应用程序退出时,JVM停止而不等待挂起的对象完成,因此实际上不能保证终结器将运行。

我同意大部分答案。

你不应该完全依赖finalize或ShutdownHook

完成

The JVM does not guarantee when this finalize() method will be invoked. finalize() gets called only once by GC thread. If an object revives itself from finalizing method, then finalize will not be called again. In your application, you may have some live objects, on which garbage collection is never invoked. Any Exception that is thrown by the finalizing method is ignored by the GC thread 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)

注册一个新的虚拟机关闭钩子。

Java虚拟机在响应两种事件时关闭:

The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or 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. 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. 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. 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

在极少数情况下,虚拟机可能会中止,即停止运行而不完全关闭

这发生在虚拟机从外部终止时,例如在Unix上使用SIGKILL信号或在Microsoft Windows上使用TerminateProcess调用。如果本机方法出错,虚拟机也可能中止,例如破坏内部数据结构或试图访问不存在的内存。如果虚拟机中止,则不能保证是否会运行任何shutdown钩子。

结论:合理使用try{} catch{} finally{}块,释放finally(}块中的关键资源。在finally{}块中释放资源时,捕获Exception和Throwable。

这里有很多很好的答案,但还有一些关于为什么应该避免使用finalize()的额外信息。

如果JVM由于System.exit()或Runtime.getRuntime().exit()而退出,终止器将不会默认运行。From Javadoc for Runtime.exit():

虚拟机的关闭顺序由两个阶段组成。在第一阶段,所有已注册的shutdown钩子(如果有的话)将以某种未指定的顺序启动,并允许并发运行,直到它们完成。在第二阶段中,如果启用了退出时终止,则运行所有未调用的终结器。完成此操作后,虚拟机将停止。

您可以调用System.runFinalization(),但它只会“尽最大努力完成所有未完成的终结”——并不能保证。

有一个System.runFinalizersOnExit()方法,但不要使用它——它是不安全的,很久以前就已经弃用了。

尽管Java的GC技术已经有了相当大的进步,但您仍然需要注意引用。我想到了许多看起来微不足道的参考模式的例子,它们实际上是罩下的老鼠窝。

从你的文章中,听起来你并不是为了对象重用而试图实现一个reset方法(真的吗?)您的对象是否持有需要清理的其他类型的资源(例如,必须关闭的流,必须返回的任何池化或借来的对象)?如果您唯一担心的是内存dealloc,那么我将重新考虑我的对象结构,并尝试验证我的对象是自包含的结构,将在GC时被清理。

不,. lang。Object#finalize是最接近的。

但是,不保证何时(以及是否)调用它。 看到:java.lang.Runtime # runFinalizersOnExit(布尔)