Loaded from: http://flyneil.iteye.com/blog/1345177
Both WeakReference and softreference can be used to hold instance references to objects, and these two classes are related to garbage collection.
WeakReference is a weak reference in which the saved object instance can be reclaimed by the GC. This class is typically used to save an object reference somewhere, without disturbing the object being recycled by GC, usually in a program such as debug, memory monitoring tools, and so on. Because such programs generally require that objects be observed, they do not affect the normal GC process of the object.
Recently, the application of Weakrefrence was also found in the implementation code of the proxy class of the JDK, which temporarily stored the dynamically generated class instance in a map composed of weakrefrence as the cache.
SoftReference is a strong reference to the object instance that it holds, and will not be reclaimed by GC unless the JVM is about to OutOfMemory. This feature makes it particularly suitable for the design object cache. For the cache, we want the cached object to always be memory-resident, but if the JVM's memory is tight, the system crashes in order not to OutOfMemoryError, allowing the JVM to reclaim the cache's memory if necessary. The data is then re-load to the cache at the appropriate time. This allows the system to be designed more flexibly.
A test procedure for WeakReference:
- Import java.lang.ref.WeakReference;
- Public class Weakreferencetest {
- /**
- * @param args
- */
- public static void Main (string[] args) {
- 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% = = 0) {
- System.GC ();
- System.out.println ("System.GC () was invoked!");
- }
- try {
- Thread.Sleep (500);
- } catch (Interruptedexception e) {
- }
- }
- System.out.println ("Object A is cleared by jvm!");
- }
- }
The result of the operation is:
- 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: ten
- System.GC () was invoked!
- Object A was cleared by jvm!
A test procedure for SoftReference:
- Import java.lang.ref.SoftReference;
- Public class Softreferencetest {
- /**
- * @param args
- */
- public static void Main (string[] args) {
- 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% = = 0) {
- System.GC ();
- System.out.println ("System.GC () was invoked!");
- }
- try {
- Thread.Sleep (500);
- } catch (Interruptedexception e) {
- }
- }
- System.out.println ("Object A is cleared by jvm!");
- }
- }
The result of the operation is:
- Get Str from Object of Softreference:hello, reference, Count: 1
- Get Str from Object of Softreference:hello, reference, Count: 2
- Get Str from Object of Softreference:hello, reference, Count: 3
- Get Str from Object of Softreference:hello, reference, Count: 4
- Get Str from Object of Softreference:hello, reference, Count: 5
- Get Str from Object of Softreference:hello, reference, Count: 6
- Get Str from Object of Softreference:hello, reference, Count: 7
- Get Str from Object of Softreference:hello, reference, Count: 8
- Get Str from Object of Softreference:hello, reference, Count: 9
- Get Str from Object of Softreference:hello, reference, Count: ten
- System.GC () was invoked!
- Get Str from Object of Softreference:hello, reference, Count: One
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- System.GC () was invoked!
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
- Get Str from Object of Softreference:hello, reference, Count:
For practical use, I use SoftReference to save objects that are cached in slices, the object references passed between the UI, and so on.
The difference between WeakReference and SoftReference