Java and manually executing finalize

前端 未结 3 2097
你的背包
你的背包 2020-12-01 16:32

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 objec

3条回答
  •  天涯浪人
    2020-12-01 17:00

    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();
    

提交回复
热议问题