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


当前回答

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

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

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

其他回答

Java中的六种对象可达性状态:

Strongly reachable objects - GC will not collect (reclaim the memory occupied by) this kind of object. These are reachable via a root node or another strongly reachable object (i.e. via local variables, class variables, instance variables, etc.) Softly reachable objects - GC may attempt to collect this kind of object depending on memory contention. These are reachable from the root via one or more soft reference objects Weakly reachable objects - GC must collect this kind of object. These are reachable from the root via one or more weak reference objects Resurrect-able objects - GC is already in the process of collecting these objects. But they may go back to one of the states - Strong/Soft/Weak by the execution of some finalizer Phantomly reachable object - GC is already in the process of collecting these objects and has determined to not be resurrect-able by any finalizer (if it declares a finalize() method itself, then its finalizer will have been run). These are reachable from the root via one or more phantom reference objects Unreachable object - An object is neither strongly, softly, weakly, nor phantom reachable, and is not resurrectable. These objects are ready for reclamation

欲了解更多详细信息:https://www.artima.com/insidejvm/ed2/gc16.html«崩溃

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

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

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

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

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

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

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

弱引用 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ref/WeakReference.html

原理:弱引用与垃圾回收相关。通常,具有一个或多个引用的对象将不符合垃圾收集的条件。 上述原则在弱参考时不适用。如果一个对象对其他对象只有弱引用,那么它就可以进行垃圾收集了。

让我们看看下面的例子:我们有一个Map with Objects,其中Key是引用一个对象。

import java.util.HashMap;   
public class Test {

    public static void main(String args[]) {
        HashMap<Employee, EmployeeVal> aMap = new 
                       HashMap<Employee, EmployeeVal>();

        Employee emp = new Employee("Vinoth");
        EmployeeVal val = new EmployeeVal("Programmer");

        aMap.put(emp, val);

        emp = null;

        System.gc();
        System.out.println("Size of Map" + aMap.size());

    }
}

现在,在程序执行期间,我们使emp = null。持有键的Map在这里没有意义,因为它是空的。在上述情况下,对象不会被垃圾收集。

WeakHashMap

在WeakHashMap中,当不再可能从Map中检索条目时,条目(键到值的映射)将被删除。

让我用WeakHashMap展示上面的例子

import java.util.WeakHashMap;

public class Test {

    public static void main(String args[]) {
        WeakHashMap<Employee, EmployeeVal> aMap = 
                    new WeakHashMap<Employee, EmployeeVal>();

        Employee emp = new Employee("Vinoth");
        EmployeeVal val = new EmployeeVal("Programmer");

        aMap.put(emp, val);

        emp = null;

        System.gc();
        int count = 0;
        while (0 != aMap.size()) {
            ++count;
            System.gc();
        }
        System.out.println("Took " + count
                + " calls to System.gc() to result in weakHashMap size of : "
                + aMap.size());
    }
}

输出:对System.gc()进行了20次调用,导致高德地图大小为:0。

WeakHashMap只有对键的弱引用,没有像其他Map类那样的强引用。虽然使用了WeakHashMap,但在某些情况下,当值或键被强引用时,您必须小心。这可以通过将对象包装在WeakReference中来避免。

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class Test {

    public static void main(String args[]) {
        HashMap<Employee, EmployeeVal> map = 
                      new HashMap<Employee, EmployeeVal>();
        WeakReference<HashMap<Employee, EmployeeVal>> aMap = 
                       new WeakReference<HashMap<Employee, EmployeeVal>>(
                map);

        map = null;

        while (null != aMap.get()) {
            aMap.get().put(new Employee("Vinoth"),
                    new EmployeeVal("Programmer"));
            System.out.println("Size of aMap " + aMap.get().size());
            System.gc();
        }
        System.out.println("Its garbage collected");
    }
}

软引用。

软引用比弱引用强一些。软引用允许垃圾回收,但只有在没有其他选择时才请求垃圾回收器清除它。

The garbage collector does not aggressively collect softly reachable objects the way it does with weakly reachable ones -- instead it only collects softly reachable objects if it really "needs" the memory. Soft references are a way of saying to the garbage collector, "As long as memory isn't too tight, I'd like to keep this object around. But if memory gets really tight, go ahead and collect it and I'll deal with that." The garbage collector is required to clear all soft references before it can throw OutOfMemoryError.

软引用和弱引用之间唯一真正的区别是

垃圾收集器使用算法来决定是否 回收软可达对象,但总是回收弱可达对象 可访问的对象。

应该注意,弱引用对象只有在只有弱引用时才会被收集。如果它有一个强引用,那么无论它有多少弱引用,它都不会被收集。