Softreference (soft reference) and weakreference (weak reference) are two classes used for Java objects in heap. These two classes can be used for simple interaction with GC.
Weakreference is a weak reference, in which the stored object instance can be recycled by GC. This class is usually used to save the object reference somewhere without interfering with GC collection of the object. It is usually used in programs such as debug and memory monitoring tools. This type of program generally requires that the object be observed and does not affect the normal GC process of the object.
Recently, weakrefrence is also found in the JDK proxy class implementation code. The proxy will save the dynamically generated class instance to a map consisting of weakrefrence as the cache.
Softreference is a strongly referenced object instance. Unless JVM is about to outofmemory, it will not be recycled by GC. This feature makes it especially suitable for designing object cache. For cache, it is best to keep the cached object in the memory. However, if the JVM memory is tight, in order to avoid system crash caused by outofmemoryerror, the JVM is allowed to reclaim the cache memory when necessary, the data will be re-loaded to the cache when appropriate. In this way, the system can be designed to be more flexible.
Strong reference
Object o=new Object(); Object o1=o;
In the above Code, the first sentence is to create an object in the heap to reference this object through O, and the second sentence is to create a reference to the object in the heap through O from O1 to new object, both references are strongly referenced. GC does not collect any reference to an object in heap. use the following code:
o=null; o1=null;
If the values of O and O1 are explicitly set to null or out of the range, GC considers that the object does not have a reference, and then it can be collected. Collection is not equal to being collected in a short time. The collection time depends on the GC algorithm, which brings a lot of uncertainty. For example, if you want to specify an object and want to collect it during the next GC run, you can't do it. With the other two references, you can do it. The other two references can be used for simple interaction without interfering with GC collection.
In heap, objects can be strong and objects, soft and objects, weak and objects, virtual and accessible objects, and non-reachable objects. The order of application strength isStrong, soft, weak, and virtual. The most powerful reference of an object is the sum of objects. As follows:
Weakreference
import java.lang.ref.WeakReference; public class WeakReferenceTest { /** * @param args */ public static void main(String[] args) { A a = new A(); a.str = "Hello, reference"; WeakReference<A> weak = new WeakReference<A>(a); a = null; int i = 0; while (weak.get() != null) { System.out.println(String.format("Get str from object of WeakReference: %s, count: %d", weak.get().str, ++i)); if (i % 10 == 0) { System.gc(); System.out.println("System.gc() was invoked!"); } try { Thread.sleep(500); } catch (InterruptedException e) { } } System.out.println("object a was cleared by JVM!"); } }
Program running result:
Get str from object of WeakReference: Hello, reference, count: 1 Get str from object of WeakReference: Hello, reference, count: 2 Get str from object of WeakReference: Hello, reference, count: 3 Get str from object of WeakReference: Hello, reference, count: 4 Get str from object of WeakReference: Hello, reference, count: 5 Get str from object of WeakReference: Hello, reference, count: 6 Get str from object of WeakReference: Hello, reference, count: 7 Get str from object of WeakReference: Hello, reference, count: 8 Get str from object of WeakReference: Hello, reference, count: 9 Get str from object of WeakReference: Hello, reference, count: 10 System.gc() was invoked! object a was cleared by JVM!
Softreference
import java.lang.ref.SoftReference; public class SoftReferenceTest { /** * @param args */ public static void main(String[] args) { A a = new A(); a.str = "Hello, reference"; SoftReference<A> sr = new SoftReference<A>(a); a = null; int i = 0; while (sr.get() != null) { System.out.println(String.format("Get str from object of SoftReference: %s, count: %d", sr.get().str, ++i)); if (i % 10 == 0) { System.gc(); System.out.println("System.gc() was invoked!"); } try { Thread.sleep(500); } catch (InterruptedException e) { } } System.out.println("object a was cleared by JVM!"); } }Summary
Softreference is more powerful than weakreference. When the JVM memory is not tight, soft can retain reference to this object even if the referenced object is left empty, at this time, the JVM memory pool actually retains the original object. Only when the memory is tight will the JVM clear the soft reference object and reload the referenced object in the future.
Weakreference automatically clears the referenced objects when the memory pool is cleared.
I am the dividing line of tiantiao
Reference: http://wiseideal.iteye.com/blog/1469295
Weakreference and softreference