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

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

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

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


当前回答

也许你可以试试……Finally块来最终确定您正在使用对象的控制流中的对象。当然,它不会自动发生,但在c++中破坏也不会发生。你经常在finally块中看到资源的关闭。

其他回答

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

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

首先,请注意,由于Java是垃圾收集的,因此很少需要对对象销毁做任何事情。首先是因为你通常没有任何托管资源可以释放,其次是因为你无法预测它何时或是否会发生,所以当你需要“一旦没有人再使用我的对象”就发生的事情是不合适的。

你可以使用java.lang.ref.PhantomReference在一个对象被销毁后得到通知(实际上,说它已经被销毁可能有点不准确,但如果对它的幻影引用处于队列中,那么它就不再是可恢复的,这通常是一样的)。常见的用法是:

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.

还有finalize(),它看起来像析构函数,但行为并不像析构函数。这通常不是一个好的选择。

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

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

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

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

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

看看try-with-resources语句。例如:

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  System.out.println(br.readLine());
} catch (Exception e) {
  ...
} finally {
  ...
}

在这里,不再需要的资源在BufferedReader.close()方法中被释放。您可以创建自己的实现AutoCloseable的类,并以类似的方式使用它。

在代码结构方面,这个语句比finalize更有局限性,但同时它使代码更易于理解和维护。此外,也不能保证在应用程序的生存期内调用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停止而不等待挂起的对象完成,因此实际上不能保证终结器将运行。