java.lang.ref.WeakReference和java.lang.ref.SoftReference有什么区别?


当前回答

在Java中;从强到弱依次为:强、软、弱、幻

强引用是一种普通引用,用于保护被引用的对象不被GC收集。即从不收集垃圾。

软引用可以被垃圾回收器收集,但可能直到需要它的内存时才会被收集。即在OutOfMemoryError之前进行垃圾收集。

弱引用是指不保护被引用对象不被GC收集的引用。即,当没有强或软引用时,垃圾收集。

幻影引用是在对象完成后,但在已分配的内存被回收之前,对对象的幻影引用。

类比:假设JVM是一个王国,对象是王国的国王,GC是王国的攻击者,试图杀死国王(对象)。

当国王强大时,GC不能杀死他。 当国王是软的,GC攻击他,但国王统治王国的保护,直到资源可用。 当国王虚弱时,GC攻击他,但在没有保护的情况下统治王国。 当国王是幻影时,GC已经杀死了他,但国王可以通过他的灵魂获得。

其他回答

Weak references are collected eagerly. If GC finds that an object is weakly reachable (reachable only through weak references), it'll clear the weak references to that object immediately. As such, they're good for keeping a reference to an object for which your program also keeps (strongly referenced) "associated information" somewere, like cached reflection information about a class, or a wrapper for an object, etc. Anything that makes no sense to keep after the object it is associated with is GC-ed. When the weak reference gets cleared, it gets enqueued in a reference queue that your code polls somewhere, and it discards the associated objects as well. That is, you keep extra information about an object, but that information is not needed once the object it refers to goes away. Actually, in certain situations you can even subclass WeakReference and keep the associated extra information about the object in the fields of the WeakReference subclass. Another typical use of WeakReference is in conjunction with Maps for keeping canonical instances.

另一方面,softreference对于缓存外部的、可重新创建的资源很有好处 因为GC通常会延迟清除它们。尽管如此,这是有保证的 在抛出OutOfMemoryError之前,sofreferferences将被清除 理论上不会引起OOME[*]。

Typical use case example is keeping a parsed form of a contents from a file. You'd implement a system where you'd load a file, parse it, and keep a SoftReference to the root object of the parsed representation. Next time you need the file, you'll try to retrieve it through the SoftReference. If you can retrieve it, you spared yourself another load/parse, and if the GC cleared it in the meantime, you reload it. That way, you utilize free memory for performance optimization, but don't risk an OOME.

现在是[*]。保持一个SoftReference本身不会导致OOME。如果 另一方面,你错误地把软参考用在了一个弱参考的任务上 (也就是说,你以某种方式保存与对象相关的信息 ,并在Reference对象获得时丢弃它 清除),您可以运行OOME作为您的代码轮询referencqueuue 而丢弃关联对象可能会发生不及时运行的情况 时尚。

所以,决定取决于使用情况 -如果你缓存的信息构造起来很昂贵,但是 尽管如此,可从其他数据重构,使用软引用 -如果您保留对某些数据的规范实例的引用,或者 您希望拥有对对象的引用,但不“拥有”它(因此 防止它被GC),使用弱引用。

SoftReference is designed for caches. When it is found that a WeakReference references an otherwise unreachable object, then it will get cleared immediately. SoftReference may be left as is. Typically there is some algorithm relating to the amount of free memory and the time last used to determine whether it should be cleared. The current Sun algorithm is to clear the reference if it has not been used in as many seconds as there are megabytes of memory free on the Java heap (configurable, server HotSpot checks against maximum possible heap as set by -Xmx). SoftReferences will be cleared before OutOfMemoryError is thrown, unless otherwise reachable.

唯一真正的区别

根据文档,松散的weakreference必须由正在运行的GC清除。

根据文档,在抛出OOM之前必须清除松散的softreference。

这是唯一真正的区别。其他的都不是合同的一部分。(我假设最新的文件是合同文件。)

软引用很有用。内存敏感的缓存使用软引用,而不是弱引用。 WeakReference的唯一正确用法是观察GC运行。为此,您可以创建一个新的WeakReference,其对象立即超出作用域,然后尝试从weak_ref.get()中获取null。当它为空时,您可以了解到在此期间GC运行了。

关于WeakReference的错误使用,列表是无限的:

a lousy hack to implement priority-2 softreference such that you don't have to write one, yet it doesn't work as expected because the cache would be cleared on every GC run, even when there is spare memory. See https://stackoverflow.com/a/3243242/632951 for phails. (Besides, what if you need more than 2 levels of cache priority? You'd still gotta need a real library for it.) a lousy hack to associate data with an object of an existing class, yet it creates a memory leak (OutOfMemoryError) when your GC decides to take a break after your weakreferences are created. Besides, it's beyond ugly: A better approach is to use tuples. a lousy hack to associate data with an object of an existing class, where the class has the nerve to make itself non-subclassable, and is used in an existing function code which you need to call. In such a case, the proper solution is to either edit the class and make it subclassable, or edit the function and make it take an interface instead of a class, or use an alternative function.

WeakReference:只弱引用的对象将在每个GC周期(minor或full)收集。

SoftReference:当只被软引用的对象被收集时,取决于:

-XX:SoftRefLRUPolicyMSPerMB=N标志(默认值是1000,也就是1秒) 堆中的空闲内存量。 例子: 堆有10MB的空闲空间(在完全GC之后); - xx: SoftRefLRUPolicyMSPerMB = 1000 如果只被SoftReference引用的对象上次被访问的时间大于10秒,则该对象将被收集。

在Java中;从强到弱依次为:强、软、弱、幻

强引用是一种普通引用,用于保护被引用的对象不被GC收集。即从不收集垃圾。

软引用可以被垃圾回收器收集,但可能直到需要它的内存时才会被收集。即在OutOfMemoryError之前进行垃圾收集。

弱引用是指不保护被引用对象不被GC收集的引用。即,当没有强或软引用时,垃圾收集。

幻影引用是在对象完成后,但在已分配的内存被回收之前,对对象的幻影引用。

类比:假设JVM是一个王国,对象是王国的国王,GC是王国的攻击者,试图杀死国王(对象)。

当国王强大时,GC不能杀死他。 当国王是软的,GC攻击他,但国王统治王国的保护,直到资源可用。 当国王虚弱时,GC攻击他,但在没有保护的情况下统治王国。 当国王是幻影时,GC已经杀死了他,但国王可以通过他的灵魂获得。