了解Java的Reference类:SoftReference,WeakReference和PhantomReference [英] Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

查看:139
本文介绍了了解Java的Reference类:SoftReference,WeakReference和PhantomReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释三个参考类之间的区别(或发布链接到一个很好的解释)? SoftReference > WeakReference > PhantomReference ,但我什么时候会使用每一个?为什么有 WeakHashMap 但没有 SoftHashMap PhantomHashMap

Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference, but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap?

如果我使用以下代码...

And if I use the following code...

WeakReference<String> ref = new WeakReference<String>("Hello!");
if (ref != null) {                 // ref can get collected at any time...
    System.gc();                   // Let's assume ref gets collected here.
    System.out.println(ref.get()); // Now what?!
}

......会发生什么?我是否必须在每个语句之前检查 ref 是否为空(这是错误的,但 我应该做什么)?对不起快速问题,但是我无法理解这些参考类......谢谢!

...what happens? Do I have to check if ref is null before every statement (this is wrong, but what should I do)? Sorry for the rapid-fire questions, but I'm having trouble understanding these Reference classes... Thanks!

推荐答案

Java库 java.lang.ref 包的文档表征三种显式引用类型的强度递减。

The Java library documentation for the java.lang.ref package characterizes the decreasing strength of the three explicit reference types.

如果希望引用的对象保持活动状态,直到主机进程内存不足,则使用 SoftReference 。在收集器需要释放内存之前,该对象将无法进行收集。松散地说,绑定一个 SoftReference 意味着,将对象固定到你不能再这样做了。

You use a SoftReference when you want the referenced object to stay alive until the host process is running low on memory. The object will not be eligible for collection until the collector needs to free memory. Loosely stated, binding a SoftReference means, "Pin the object until you can't anymore."

相比之下,当你不想影响引用对象的生命周期时,使用 WeakReference ;你只想做一个单独的断言关于引用的对象,只要它仍然存活。对象的收集资格不受绑定 WeakReference s的影响。类似于从对象实例到相关属性的外部映射,只要相关对象处于活动状态,只需要记录属性,这对于 WeakReference WeakHashMap

By contrast, use a WeakReference when you don't want to influence the referenced object's lifetime; you merely want to make a separate assertion about the referenced object, so long as it remains alive. The object's eligibility for collection is not influenced by the presence of bound WeakReferences. Something like a an external mapping from object instance to related property, where the property need only be recorded so long as the related object is alive, is a good use for WeakReferences and WeakHashMap.

最后一个 - PhantomReference -is更难刻画。与 WeakReference 一样,这样的绑定 PhantomReference 对引用对象的生命周期没有任何影响。但与其他引用类型不同,人们甚至无法取消引用 PhantomReference 。从某种意义上说,就呼叫者而言,它并不指向它指向的东西。它只允许将一些相关数据与引用的对象相关联 - 当 PhantomReference 在其相关的的ReferenceQueue 。通常,从 PhantomReference 派生类型,并在该派生类型中包含一些其他数据。不幸的是,有一些向下转换需要使用这样的派生类型。

The last one—PhantomReference—is harder to characterize. Like WeakReference, such a bound PhantomReference exerts no influence on the referenced object's lifetime. But unlike the other reference types, one can't even dereference a PhantomReference. In a sense, it doesn't point to the thing it points to, as far as callers can tell. It merely allows one to associate some related data with the referenced object -- data that can later be inspected and acted upon when the PhantomReference gets queued in its related ReferenceQueue. Normally one derives a type from PhantomReference and includes some additional data in that derived type. Unfortunately, there's some downcasting involved to make use of such a derived type.

在你的示例代码中,它不是 ref 引用(或者,如果您愿意,变量)可以为null。相反,它是通过调用 参考#get() 可能为null。如果发现它是空的,那你就太晚了;引用的对象已经被收集了:

In your example code, it's not the ref reference (or, if you prefer, "variable") that can be null. Rather, it's the value obtained by calling Reference#get() that may be null. If it is found to be null, you're too late; the referenced object is already on its way to being collected:

final String val = ref.get();
if (null != val)
{
  // "val" is now pinned strongly.
}
else
{
  // "val" is already ready to be collected.
}

这篇关于了解Java的Reference类:SoftReference,WeakReference和PhantomReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆