Java和手动执行finalize

发布于 2024-07-04 14:03:56 字数 329 浏览 7 评论 0原文

如果我在程序代码中对某个对象调用 finalize(),当垃圾收集器处理该对象时,JVM 是否仍会再次运行该方法?

这是一个大概的示例:

MyObject m = new MyObject();

m.finalize();

m = null;

System.gc()

显式调用 finalize() 是否会使 JVM 的垃圾收集器不运行 finalize()对象 m 上的方法?

If I call finalize() on an object from my program code, will the JVM still run the method again when the garbage collector processes this object?

This would be an approximate example:

MyObject m = new MyObject();

m.finalize();

m = null;

System.gc()

Would the explicit call to finalize() make the JVM's garbage collector not to run the finalize() method on object m?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪荡不羁 2024-07-11 14:03:56

根据这个简单的测试程序,即使您显式调用 Finalize(),JVM 仍然会调用它:

private static class Blah
{
  public void finalize() { System.out.println("finalizing!"); }
}

private static void f() throws Throwable
{
   Blah blah = new Blah();
   blah.finalize();
}

public static void main(String[] args) throws Throwable
{
    System.out.println("start");
    f();
    System.gc();
    System.out.println("done");
}

输出为:

开始
敲定!
敲定!
完成

永远不要显式调用 Finalize(),而且几乎永远不要实现该方法,因为无法保证是否以及何时调用它。 您最好手动关闭所有资源。

According to this simple test program, the JVM will still make its call to finalize() even if you explicitly called it:

private static class Blah
{
  public void finalize() { System.out.println("finalizing!"); }
}

private static void f() throws Throwable
{
   Blah blah = new Blah();
   blah.finalize();
}

public static void main(String[] args) throws Throwable
{
    System.out.println("start");
    f();
    System.gc();
    System.out.println("done");
}

The output is:

start
finalizing!
finalizing!
done

Every resource out there says to never call finalize() explicitly, and pretty much never even implement the method because there are no guarantees as to if and when it will be called. You're better off just closing all of your resources manually.

苏璃陌 2024-07-11 14:03:56

要理解finalize的功能,就必须了解垃圾收集器(GC)工作流程。 调用 .finalize() 不会调用垃圾收集器,也不会调用 system.gc。 实际上,finalize 对编码器有帮助的是将对象的引用声明为“未引用”。

GC 会强制暂停 JVM 的运行,从而降低性能。 在运行过程中,GC会从根对象(你的main方法调用)开始遍历所有引用的对象。 可以通过手动将对象声明为未引用来减少此挂起时间,因为它将减少通过自动运行来声明对象引用已过时的操作成本。 通过声明finalize(),编码器将对象的引用设置为废弃,因此在下次运行GC操作时,GC运行将在不使用操作时间的情况下扫描对象。

引用:“对一个对象调用finalize方法后,不会采取进一步的操作,直到Java虚拟机再次确定任何尚未死亡的线程不再可以通过任何方式访问该对象,包括准备完成的其他对象或类的可能操作,此时该对象可能会被丢弃”,来自 Java API Doc on java.Object.finalize();

有关详细说明,您还可以查看:javabook.computerware

One must understand the Garbage Collector(GC) Workflow to understand the function of finalize. calling .finalize() will not invoke the garbage collector, nor calling system.gc. Actually, What finalize will help the coder is to declare the reference of the object as "unreferenced".

GC forces a suspension on the running operation of JVM, which creates a dent on the performance. During operation, GC will traverse all referenced objects, starting from the root object(your main method call). This suspension time can be decreased by declaring the objects as unreferenced manually, because it will cut down the operation costs to declare the object reference obsolete by the automated run. By declaring finalize(), coder sets the reference to the object obsolete, thus on the next run of GC operation, GC run will sweep the objects without using operation time.

Quote: "After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded. " from Java API Doc on java.Object.finalize();

For detailed explanation, you can also check: javabook.computerware

想挽留 2024-07-11 14:03:56

对于任何给定的对象,JVM 永远不会多次调用 Finalize 方法。 无论如何,您不应该依赖于 Finalize,因为不能保证它会被调用。 如果您调用 Finalize 是因为需要执行清理代码,那么最好将其放入单独的方法中并使其明确,例如:

public void cleanUp() {
  .
  .
  .
}

myInstance.cleanUp();

The finalize method is never invoked more than once by a JVM for any given object. You shouldn't be relying on finalize anyway because there's no guarantee that it will be invoked. If you're calling finalize because you need to execute clean up code then better to put it into a separate method and make it explicit, e.g:

public void cleanUp() {
  .
  .
  .
}

myInstance.cleanUp();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文