The difference between WeakReference and SoftReference

Source: Internet
Author: User

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:

  1. Import java.lang.ref.WeakReference;
  2. Public class Weakreferencetest {
  3. /** 
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. A = new A ();
  8. A.str = "Hello, reference";
  9. weakreference<a> weak = new weakreference<a> (A);
  10. A = null;
  11. int i = 0;
  12. While (weak.get () = null) {
  13. System.out.println (String.Format ("Get str from Object of WeakReference:%s, Count:%d", Weak.get (). STR, ++i));
  14. if (i% = = 0) {
  15. System.GC ();
  16. System.out.println ("System.GC () was invoked!");
  17. }
  18. try {
  19. Thread.Sleep (500);
  20. } catch (Interruptedexception e) {
  21. }
  22. }
  23. System.out.println ("Object A is cleared by jvm!");
  24. }
  25. }

The result of the operation is:

  1. Get Str from Object of Weakreference:hello, reference, Count: 1
  2. Get Str from Object of Weakreference:hello, reference, Count: 2
  3. Get Str from Object of Weakreference:hello, reference, Count: 3
  4. Get Str from Object of Weakreference:hello, reference, Count: 4
  5. Get Str from Object of Weakreference:hello, reference, Count: 5
  6. Get Str from Object of Weakreference:hello, reference, Count: 6
  7. Get Str from Object of Weakreference:hello, reference, Count: 7
  8. Get Str from Object of Weakreference:hello, reference, Count: 8
  9. Get Str from Object of Weakreference:hello, reference, Count: 9
  10. Get Str from Object of Weakreference:hello, reference, Count: ten
  11. System.GC () was invoked!
  12. Object A was cleared by jvm!

A test procedure for SoftReference:

  1. Import java.lang.ref.SoftReference;
  2. Public class Softreferencetest {
  3. /** 
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. A = new A ();
  8. A.str = "Hello, reference";
  9. softreference<a> sr = New softreference<a> (A);
  10. A = null;
  11. int i = 0;
  12. While (sr.get () = null) {
  13. System.out.println (String.Format ("Get str from Object of SoftReference:%s, Count:%d", Sr.get (). STR, ++i));
  14. if (i% = = 0) {
  15. System.GC ();
  16. System.out.println ("System.GC () was invoked!");
  17. }
  18. try {
  19. Thread.Sleep (500);
  20. } catch (Interruptedexception e) {
  21. }
  22. }
  23. System.out.println ("Object A is cleared by jvm!");
  24. }
  25. }

The result of the operation is:

  1. Get Str from Object of Softreference:hello, reference, Count: 1
  2. Get Str from Object of Softreference:hello, reference, Count: 2
  3. Get Str from Object of Softreference:hello, reference, Count: 3
  4. Get Str from Object of Softreference:hello, reference, Count: 4
  5. Get Str from Object of Softreference:hello, reference, Count: 5
  6. Get Str from Object of Softreference:hello, reference, Count: 6
  7. Get Str from Object of Softreference:hello, reference, Count: 7
  8. Get Str from Object of Softreference:hello, reference, Count: 8
  9. Get Str from Object of Softreference:hello, reference, Count: 9
  10. Get Str from Object of Softreference:hello, reference, Count: ten
  11. System.GC () was invoked!
  12. Get Str from Object of Softreference:hello, reference, Count: One
  13. Get Str from Object of Softreference:hello, reference, Count:
  14. Get Str from Object of Softreference:hello, reference, Count:
  15. Get Str from Object of Softreference:hello, reference, Count:
  16. Get Str from Object of Softreference:hello, reference, Count:
  17. Get Str from Object of Softreference:hello, reference, Count:
  18. Get Str from Object of Softreference:hello, reference, Count:
  19. Get Str from Object of Softreference:hello, reference, Count:
  20. Get Str from Object of Softreference:hello, reference, Count:
  21. Get Str from Object of Softreference:hello, reference, Count:
  22. System.GC () was invoked!
  23. Get Str from Object of Softreference:hello, reference, Count:
  24. Get Str from Object of Softreference:hello, reference, Count:
  25. Get Str from Object of Softreference:hello, reference, Count:
  26. Get Str from Object of Softreference:hello, reference, Count:
  27. Get Str from Object of Softreference:hello, reference, Count:
  28. Get Str from Object of Softreference:hello, reference, Count:
  29. Get Str from Object of Softreference:hello, reference, Count:
  30. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.