Soft references for Java memory management (Soft Reference)

Source: Internet
Author: User
Tags garbage collection thread java throws

The main feature of soft reference (Soft Reference) is that it has strong reference function. This type of memory is recycled only when there is not enough memory, so they are usually not recycled when memory is sufficient. In addition, these reference objects are guaranteed to be set to NULL before Java throws OutOfMemory exceptions. He can be used to implement some common resources of caching, to achieve the cache function, to ensure maximum use of memory without causing outofmemory anomalies.

The following is the implementation code for the soft reference:

 import java.lang.ref.SoftReference;  
        public class SoftReference {/** * @param args */public static void main (string[] args) {  
              
        TODO auto-generated Method stub A A = new A ();  
      
        Use a a.test ();  
        Finished using a, set it to the soft reference type, and release the strong reference softreference sr = new SoftReference (a);  
              
        A = null;  
            Next use if (SR!= null) {a = (a) sr.get ();  
        A.test ();  
            else {//GC has released a because of low memory, so it is necessary to reload A = new A ();  
            A.test ();  
            A = null;  
        sr = new SoftReference (a);  
    Class A {public void Test () {System.out.println ("Soft Reference test"); }  
}

The introduction of soft reference technology enables Java applications to better manage memory, stabilize the system, prevent system memory overflow, and avoid system crashes. This technique should be applied as much as possible when dealing with objects that are large in memory and have a long declaration cycle but use infrequently. But things always have two sides, pros and cons, in some cases the use of soft references will reduce the operational efficiency and performance of the application, for example: the application of soft reference to the initialization of the object is more time-consuming, or the object's state in the process of running the program changed, will be recreated objects and initialization objects Bring different degrees of trouble, sometimes we have to weigh the pros and cons of the application.

This article is from the Java World: http://www.blogjava.net/ajie/archive/2005/12/18/24435.html

Let's look at how to use soft references in Android

The SoftReference in Java
Is the soft reference of the object. If an object has a soft reference and the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is low, the memory of those objects is reclaimed. The object can be used by the program as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive caching. Using soft references can prevent memory leaks and enhance the robustness of your programs.
The SoftReference feature is that an instance of it holds soft references to a Java object that does not prevent garbage collection threads from reclaiming the Java object. That is, once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread reclaims the Java object. Also, once the garbage thread reclaims the Java object, the Get () method returns null

Caching soft reference bitmap objects with the map collection

map<string, softreference<bitmap>> imagecache = new New hashmap<string, 

Softreference<bitmap >> ();  
Strongly referenced Bitmap object  
Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);  
Soft reference Bitmap Object  
softreference<bitmap> bitmapcache = new softreference<bitmap> (Bitmap);  
Add the object to the map so that it caches  
imagecache.put ("1", softrbitmap);  
      
      
The Bitmap object that takes soft references from the cache  
softreference<bitmap> bitmapcache_ = Imagecache.get ("1");  
Remove the Bitmap object, if the Bitmap is recycled due to insufficient memory, will get empty  
      
Bitmap Bitmap_ = Bitmapcache_.get ();

If you need to load a large number of pictures from the Web at this point, consider using the sdcard to create a temporary folder cache of these pictures.

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.