问题描述
有没有可以在java中标记一个对象来进行下次清理时垃圾回收?
Is there away that I can mark an object, in java, to be garbage collected for the next time it cleans up?
我听说将对象设置为null不再有效。
I've heard that setting the object to null no longer works.
推荐答案
我知道这个问题已经得到解答,但您可以通过多种方式来操作Java垃圾收集器查看您的方式引用。您可以通过软参考,弱参考和幻影参考来做到这一点。查看java.lang.ref包以获得更好的解释。
I know this question has been answered, but there are ways you can manipulate the way that the Java Garbage collector looks at your references. You can do this through Soft References, Weak References, and Phantom References. Take a look at the java.lang.ref package for a better explanation.
此处还有一个很好的示例,用于确定PhantomReference何时将被垃圾收集。
Also here's a nice sample to determine when a PhantomReference is about to be garbage collected.
以下是它的主要网站:
这篇关于在GC中标记要删除的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!