有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    1. “Finalizer”类java.lang.ref.Finalizer和“Finalizer()”方法java.object.finalize是两个独立的、不同的东西

    2. 正如alexey提到的“finalize()”,

    You can override [finalize()] ... to specify what to do when a given object is collected by garbage collector.

    1. 这里讨论了“终结器类”的目的、作用和生命周期:

    http://www.fasterj.com/articles/finalizer2.shtml

    The JVM will ignore a trivial finalize() method ... Otherwise, if an instance is being created, and that instance has a non-trivial finalize() method defined or inherited, then the JVM will do the following:

    • The JVM will create the instance

    • The JVM will also create an instance of the java.lang.ref.Finalizer class, pointing to that object instance just created (and pointing to a queue that it will be put on by the GC)

    • The java.lang.ref.Finalizer class holds on to the java.lang.ref.Finalizer instance that was just created (so that it is kept alive, otherwise nothing would keep it alive and it would be
      GCed at the next GC).

    1. 所以要回答你的问题:“finalize()”是你要做的事情(在GC期间定制对象的清理行为);“Finalize”是JVM创建的一个对象(用于促进和管理清理)

    2. 出于许多原因,包括performancesecurity,创建自己的定制终结器通常是一个糟糕的想法,除非你必须这样做

  2. # 2 楼答案

    关于方法finalize()请查看javadoc

    Called by the garbage collector on an object (that object may be any object that is why it's declared in the object class and not in the finalizer class) when garbage collection determines that there are no more references to the object.

    可以在任何类上重写该方法,以指定垃圾收集器收集给定对象时要执行的操作