首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java的ReferenceQueue

使用Java的ReferenceQueue
EN

Stack Overflow用户
提问于 2013-01-22 10:17:20
回答 4查看 21.2K关注 0票数 26

SoftReferenceWeakReference真的只在作为实例变量创建时才有帮助吗?在方法作用域中使用它们有什么好处吗?

另一个重要的部分是ReferenceQueue。除了能够跟踪哪些引用被确定为垃圾之外,是否可以使用Reference.enqueue()强制注册对象以进行垃圾收集?

例如,是否值得创建一个在对象中占用大量内存资源(由强引用持有)的方法,并创建引用来将它们排入队列?

代码语言:javascript
复制
Object bigObject;
public void dispose() {
    ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
    WeakReference<Object> ref = new WeakReference<Object>(bigObject, queue);
    bigObject = null;
    ref.enqueue();
}

(假设本例中的对象表示一个使用大量内存的对象类型……就像BufferedImage之类的东西)

这有什么实际效果吗?或者这只是一种代码的浪费?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-01-22 10:38:49

引用队列的一个常见习惯用法是,例如,子类WeakReference附加清理任务所需的信息,然后轮询ReferenceQueue以获取清理任务。

代码语言:javascript
复制
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实现的内部。

票数 36
EN

Stack Overflow用户

发布于 2013-01-22 10:24:32

如果一个对象只有WeakReference(或者没有任何引用!)因此,每当Java需要在内存中腾出更多空间时,都可以对其进行垃圾回收。所以,只要你想让一个对象留在内存中,但又不那么需要它,你就可以使用Java (例如,如果WeakReference需要对它进行垃圾回收,没问题,你可以以某种方式把它取回来,同时Java有更好的性能)

WeakReference排入队列允许您迭代ReferenceQueue并确定哪些引用已被垃圾收集,哪些未被垃圾收集。这就是全部-所以只有当你需要知道这一点时才去做。

阅读更多:http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

票数 7
EN

Stack Overflow用户

发布于 2014-04-26 21:43:24

一种常见的做法是创建软引用的映射。

代码语言:javascript
复制
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包可能有更好的方法来做到这一点。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14450538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档