我需要知道何时finalize()调用该方法JVM.我创建了一个测试类,当finalize()通过覆盖它来调用方法时,该测试类会写入文件.它没有被执行.谁能告诉我它没有执行的原因?
至于我的理解是finalize()和GC是两个不同的方面.GC使用finalize()方法释放对象内存.我们无法说明何时会发生GC(即使我们明确地调用System.gc()).但是我们可以在Object上显式调用finalize().
Will the function be executed immediately(memory freed) or it waits till GC
occurs like System.gc() call?
Run Code Online (Sandbox Code Playgroud)
此外,根据文档,对于任何给定对象,Java虚拟机永远不会多次调用finalize方法.
那么当我们先调用finalize()并在稍后的时间调用GC时会发生什么.
If object memory is not freed on explicit call to object.finalize() then would't
it being called again in the GC process violate the calling only once rule?
Run Code Online (Sandbox Code Playgroud)