在一次采访中我被问到,假设 JVM 在没有使用 A 类对象时运行 gc。
class A{
//some code here
protected void finalize(){
//code here
}
}
是否保证finalize()的执行。我说是的
接下来的问题是,如果A类的obj正在被使用,如果现在JVM运行GC,它是否会执行finalize()。我说不,它不会执行这个finalize(),因为JVM不收集A的对象。
但是她没有发表任何评论,只是看起来很失望。
我理解错了吗?提前致谢
最佳答案
不能保证类 finalize()方法将被调用。
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized
关于java - Finalize() 何时执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20177580/