Strong,Soft,Weak,Phantom Reference

区分一下Strong,Soft,Weak,Phantom Reference

Strong Reference: Java 默认的Reference,强引用当引用存在时,object不会被回收;
Soft Reference:当内存不得不需要释放的时候,软引用才会被释放,并不会在每一次GC都释放; 在内存紧张时可能会被回收,不过也可以通过-XX:SoftRefLRUPolicyMSPerMB参数控制回收的时机.
Weak Reference:每一次GC都释放;
Phantom Reference: 虚引用,没看懂

下面的英文资料讲的特别好,不翻译;

Strong Reference: We use Strong references in Java everywhere: we can create an object and then assign it to a reference. Note that if the object has a strong reference, this object is never be garbage collected.
Example:

HelloWorld hello = new HelloWorld();
Here hello is the strong reference to HelloWorld Object.

Soft Reference: If an object has no strong reference but has a soft reference, then the garbage collector reclaims this object’s memory when GC needs to free up some memory. To get Object from a soft reference, one can invoke the get() method. If the object is not GCed, it returns the object, otherwise , it returns null.All soft references to softly reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError.

Weak Reference: If an object has no strong reference but has a weak reference then GC reclaims this object’s memory in next run even though there is enough memory.

Phantom Reference: If an object does not have any of the above references then it may have a phantom reference. Phantom references can’t be accessed directly. When using a get() method it will always return null.

Phantom Reference can be used in situations, where sometimes using finalize() is not sensible.This is a special reference which says that the object was already finalized, and the garbage collector is ready to reclaim its memory.

参考资料

1.弱引用和软引用WeakReference,SoftReference,最简讲解,以及一个应用场景
2.Different Types of References in Java


原文地址:https://www.cnblogs.com/lijianming180/p/12409454.html