SoftReference
和WeakReference
真的只在作为实例变量创建时才有帮助吗?在方法作用域中使用它们有什么好处吗?
另一个重要的部分是ReferenceQueue
。除了能够跟踪哪些引用被确定为垃圾之外,是否可以使用Reference.enqueue()
强制注册对象以进行垃圾收集?
例如,是否值得创建一个在对象中占用大量内存资源(由强引用持有)的方法,并创建引用来将它们排入队列?
Object bigObject;
public void dispose() {
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
WeakReference<Object> ref = new WeakReference<Object>(bigObject, queue);
bigObject = null;
ref.enqueue();
}
(假设本例中的对象表示一个使用大量内存的对象类型……就像BufferedImage
之类的东西)
这有什么实际效果吗?或者这只是一种代码的浪费?
发布于 2013-01-22 10:38:49
引用队列的一个常见习惯用法是,例如,子类WeakReference
附加清理任务所需的信息,然后轮询ReferenceQueue
以获取清理任务。
ReferenceQueue<Foo> fooQueue = new ReferenceQueue<Foo>();
class ReferenceWithCleanup extends WeakReference<Foo> {
Bar bar;
ReferenceWithCleanup(Foo foo, Bar bar) {
super(foo, fooQueue);
this.bar = bar;
}
public void cleanUp() {
bar.cleanUp();
}
}
public Thread cleanupThread = new Thread() {
public void run() {
while(true) {
ReferenceWithCleanup ref = (ReferenceWithCleanup)fooQueue.remove();
ref.cleanUp();
}
}
}
public void doStuff() {
cleanupThread.start();
Foo foo = new Foo();
Bar bar = new Bar();
ReferenceWithCleanup ref = new ReferenceWithCleanup(foo, bar);
... // From now on, once you release all non-weak references to foo,
// then at some indeterminate point in the future, bar.cleanUp() will
// be run. You can force it by calling ref.enqueue().
}
例如,当weakKeys
被选择uses这种方法时,Guava的CacheBuilder
实现的内部。
发布于 2013-01-22 10:24:32
如果一个对象只有WeakReference
(或者没有任何引用!)因此,每当Java需要在内存中腾出更多空间时,都可以对其进行垃圾回收。所以,只要你想让一个对象留在内存中,但又不那么需要它,你就可以使用Java (例如,如果WeakReference
需要对它进行垃圾回收,没问题,你可以以某种方式把它取回来,同时Java有更好的性能)
将WeakReference
排入队列允许您迭代ReferenceQueue
并确定哪些引用已被垃圾收集,哪些未被垃圾收集。这就是全部-所以只有当你需要知道这一点时才去做。
阅读更多:http://weblogs.java.net/blog/2006/05/04/understanding-weak-references
发布于 2014-04-26 21:43:24
一种常见的做法是创建软引用的映射。
Map<String, SoftReference<BigThing>> cache = new HashMap<>();
Set<String> thingsIAmCurrentlyGetting = new HashSet<String>();
Object mutex = new Object();
BigThing getThing(String key) {
synchronized(mutex) {
while(thingsIAmCurrentlyGetting.contains(key)) {
mutex.wait();
}
SoftReference<BigThing> ref = cache.get(key);
BigThing bigThing = ref == null ? null : ref.get();
if(bigThing != null) return bigThing;
thingsIAmCurrentlyGetting.add(key);
}
BigThing bigThing = getBigThing(key); // this may take a while to run.
synchronized(mutex) {
cache.put(key, bigThing);
thingsIAmCurrentlyGetting.remove(key);
mutex.notifyAll();
}
return bigThing;
}
我在这里展示了我的老校友-新的java包可能有更好的方法来做到这一点。
https://stackoverflow.com/questions/14450538
复制相似问题