WeakReference.get() return 在没有强引用后可以非空吗?
Can WeakReference.get() return non-null after their are no strong references left?
在下面的代码片段中,widgetRef.get()
到 return 非空值 在 对 [ 的最后一个强可达引用之后在技术上是否可行=14=]消失了?
private class Foo {
private final WeakReference<Widget> widgetRef;
public Foo(WeakReference<Widget> widgetRef) {
this.widgetRef = widgetRef;
}
public bar() {
final Widget widget = widgetRef.get();
if (widget != null) {
widget.stuff();
}
}
}
换句话说,您能否依靠垃圾收集器在对象变得弱可达时立即处理所有 weak/soft/phantom 引用?
我相信您不能指望垃圾收集器执行此操作,因为它可能随时 运行(并且不会一直 运行ning)。 WeakReference documentation 意味着弱引用仅在垃圾收集器 运行s:
时处理
Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references.
是否有关于此行为的更明确的文档?
In the following snippet, is it technically possible for widgetRef,get() to return a non-null value after the last strong reachable reference to the Widget has disappeared?
不仅技术上可行,实践中也很有可能发生。您应该为对象实现某种生命周期,使其在结束时进入 detached/closed 状态。如果它只有一个所有者,那么该所有者应该对此负责。如果它有多个所有者,您可以使用计数器或所有者列表,基本上实现引用计数。
垃圾收集器仅用于管理内存,不用于管理其他资源或应用程序级生命周期。
In other words, can you rely on the garbage collector immediately handling all weak/soft/phantom references as soon as an object becomes weakly reachable?
没有
Is there some more explicit documentation about this behaviour?
对于您提出的问题,您认为您引用的文档的哪一部分不明确?措辞 "at a certain point in time" 可以读作 "does not guarantee immediate action".
在下面的代码片段中,widgetRef.get()
到 return 非空值 在 对 [ 的最后一个强可达引用之后在技术上是否可行=14=]消失了?
private class Foo {
private final WeakReference<Widget> widgetRef;
public Foo(WeakReference<Widget> widgetRef) {
this.widgetRef = widgetRef;
}
public bar() {
final Widget widget = widgetRef.get();
if (widget != null) {
widget.stuff();
}
}
}
换句话说,您能否依靠垃圾收集器在对象变得弱可达时立即处理所有 weak/soft/phantom 引用?
我相信您不能指望垃圾收集器执行此操作,因为它可能随时 运行(并且不会一直 运行ning)。 WeakReference documentation 意味着弱引用仅在垃圾收集器 运行s:
时处理Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references.
是否有关于此行为的更明确的文档?
In the following snippet, is it technically possible for widgetRef,get() to return a non-null value after the last strong reachable reference to the Widget has disappeared?
不仅技术上可行,实践中也很有可能发生。您应该为对象实现某种生命周期,使其在结束时进入 detached/closed 状态。如果它只有一个所有者,那么该所有者应该对此负责。如果它有多个所有者,您可以使用计数器或所有者列表,基本上实现引用计数。
垃圾收集器仅用于管理内存,不用于管理其他资源或应用程序级生命周期。
In other words, can you rely on the garbage collector immediately handling all weak/soft/phantom references as soon as an object becomes weakly reachable?
没有
Is there some more explicit documentation about this behaviour?
对于您提出的问题,您认为您引用的文档的哪一部分不明确?措辞 "at a certain point in time" 可以读作 "does not guarantee immediate action".