简体   繁体   English

在销毁“无法访问”对象之前,垃圾收集器是否总是调用“finalize()”方法?

[英]Is 'finalize()' method always called by garbage collector before destroying an 'Unreachable' object?

I red from the following link: garbage collection from geeksforgeeks and here it is said that:我从以下链接发红:来自 geeksforgeeks 的垃圾收集,这里说:

The finalize() method is never invoked more than once for any given object.对于任何给定的对象,finalize() 方法永远不会被调用多次。

So, here it is said "more than once" and i'm wondering whether there is possibility that finalize() method is not invoked and garbage collector destroys that object.所以,这里说“不止一次”,我想知道是否有可能不调用finalize()方法并且垃圾收集器销毁该对象。

Is it possible?是否可以?

When the GC has found an object with class where finalize() has been overridden it is added to a queue of objects to have finalize() called on them.当 GC 发现一个对象的类的finalize()已被覆盖时,它将被添加到对象队列中,以便对它们调用 finalize()。 It is only after the object has been finalized once, that the GC can clean it up.只有在对象完成一次后,GC 才能清理它。 ie this would be on a later GC.即这将在以后的 GC 上进行。

eg If an object is in tenured space, it might be found until a full collection is performed, and it will only be cleaned up on a full GC after the finalize method has been called.例如,如果一个对象在终身空间中,它可能会在执行完整收集之前被找到,并且只有在调用 finalize 方法之后才会在完整 GC 上被清除。

For further details, this is the Java 11 Javadoc for Object.finalize()有关更多详细信息,这是Object.finalize()的 Java 11 Javadoc

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#finalize() https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#finalize()

so is there any possibility that garbage collector fully destroyed the object in heap but didn't call the finalize() method on that object?那么垃圾收集器是否有可能完全销毁堆中的对象但没有调用该对象的 finalize() 方法?

While the object is in the finalization queue, it can't be removed.当对象在终结队列中时,它不能被删除。

And you all assumed that there is not certainity in the destroying of object by the garbage collector.并且你们都假设垃圾收集器销毁对象是不确定的。

It won't be destroyed while there is still a strong reference to it.它不会被销毁,而仍然有强烈的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM