我是否需要保留对仅用于执行终结的 WeakReference 的强引用?

Do I need to keep a strong reference to a WeakReference that is only used to perform finalization?

我想使用 Wea​​kReference 作为更高效的 finalize() 方法,目的是在可能的情况下尽快释放与对象关联的本机资源,而不使用终结(这对比使用 Wea​​kReference 的成本更高)。

因为这是 WeakReference 的唯一目的(我永远不会使用 Wea​​kReference 来获取引用的对象),花时间 space 维护我的 WeakReferences 列表以防止他们免于被垃圾收集。

但是如果构造了一个普通对象并且没有保留对它的强引用,它将被垃圾收集器简单地释放,而且我在 Javadoc 中找不到任何表明这与 WeakReference 不同的内容。

是否有必要保留对 WeakReference 的引用以防止它被垃圾收集,或者,如果它要在 ReferenceQueue 中排队,是否会在从队列中收集它之前一直保持活动状态?

Is it necessary to keep a reference to a WeakReference to prevent it from being garbage collected

看看 java.lang.ref package description,它用一整段来回答你的问题:

The relationship between a registered reference object and its queue is one-sided. That is, a queue does not keep track of the references that are registered with it. If a registered reference becomes unreachable itself, then it will never be enqueued. It is the responsibility of the program using reference objects to ensure that the objects remain reachable for as long as the program is interested in their referents.