在 Java 中排除阻止对象垃圾回收的引用
Exlude a reference from blocking garbage collection of an object in Java
我在 Java 中有一个列表,它允许在多个地方使用同一个对象。 (Table 行,因此它们不会在内存中多次缓存)。
有没有办法告诉垃圾收集器,他可以删除只被这个列表引用的对象,而不是其他任何地方?
或者像这样共享对象有更好的设计吗?
Is there a way to tell the garbage collector, that he is allowed to remove object that are only referenced by this List, and nowhere else?
有点。你想把它变成 List<WeakReference<T>>
或类似的东西。请参阅 WeakReference
和 SoftReference
的文档,详细了解它们各自抑制收集的程度。
现在当你使用那个列表的元素时,你需要检查WeakReference
or SoftReference
中的值是否已经被收集,所以它不是透明的变化 - 但我认为这就是你要找的。
我在 Java 中有一个列表,它允许在多个地方使用同一个对象。 (Table 行,因此它们不会在内存中多次缓存)。
有没有办法告诉垃圾收集器,他可以删除只被这个列表引用的对象,而不是其他任何地方?
或者像这样共享对象有更好的设计吗?
Is there a way to tell the garbage collector, that he is allowed to remove object that are only referenced by this List, and nowhere else?
有点。你想把它变成 List<WeakReference<T>>
或类似的东西。请参阅 WeakReference
和 SoftReference
的文档,详细了解它们各自抑制收集的程度。
现在当你使用那个列表的元素时,你需要检查WeakReference
or SoftReference
中的值是否已经被收集,所以它不是透明的变化 - 但我认为这就是你要找的。