Use soft references and weak references to prevent memory overflow.
The following describes how to use soft references. The usage of weak references is similar to that of soft references. Assume that our app will use a large number of default images, such as the app's default profile pictures and default game icons. These images will be used in many places. If you read an image each time, hardware operations are required to read the file, resulting in low performance. Therefore, we want to cache the image and read it directly from the memory when necessary. However, because the image occupies a large amount of memory and many images need a lot of memory to cache, The OutOfMemory exception may easily occur. At this time, we can consider using soft reference technology to avoid this problem. First, define a HashMap to save the soft reference object. Private Map <String, SoftReference <Bitmap> imageCache = new HashMap <String, SoftReference <Bitmap> (); then define a method to save the soft reference of Bitmap to HashMap. Public void addBitmapToCache (String path) {// Bitmap object with strong reference Bitmap bitmap = BitmapFactory. decodeFile (path); // soft referenced Bitmap object SoftReference <Bitmap> softBitmap = new SoftReference <Bitmap> (bitmap); // Add this object to Map to cache imageCache. when you get put (path, softBitmap);}, you can use the get () method of SoftReference to obtain the Bitmap object. Public Bitmap getBitmapByPath (String path) {// obtain the soft referenced Bitmap object SoftReference from the cache <Bitmap> softBitmap = imageCache. get (path); // determines whether soft reference if (softBitmap = null) {return null;} // retrieves a Bitmap object. if Bitmap is recycled due to insufficient memory, an empty Bitmap bitmap = softBitmap will be obtained. get (); return bitmap;} after soft reference, the memory space of these cached image resources can be released before the OutOfMemory exception occurs, so as to avoid Memory exceeding the upper limit, avoid Crash. Note that, before the Garbage Collector recycles this Java object, the get method provided by the SoftReference class will return a strong reference to the Java object. Once the garbage thread recycles this Java object, the get method returns null. Therefore, in the Code for obtaining soft reference objects, you must determine whether the value is null to avoid application crash due to NullPointerException. Experience Sharing: When should I use soft references or weak references? In my opinion, if you only want to avoid the occurrence of an OutOfMemory exception, you can use soft references. If you are more concerned about application performance and want to recycle objects that occupy a large amount of memory as soon as possible, you can use weak references. In addition, you can determine whether the object is frequently used. If this object may be frequently used, try to use soft references. If this object is not used more likely, you can use weak references. In addition, WeakHashMap is similar to the weak reference function. For a given key, WeakHashMap does not prevent the garbage collector from recycling the key. After the key is recycled, its entries are effectively removed from the ing. WeakHashMap implements this mechanism using ReferenceQueue.
Map<String, SoftReference<Bitmap>> iconCache=new HashMap<String, SoftReference<Bitmap>>();if (iconCache.containsKey(iconname)) {SoftReference<Bitmap> softref = iconCache.get(iconname);if (softref != null) { Bitmap bitmap = softref.get(); if (bitmap != null) { iv_book.setImageBitmap(bitmap); } else { loadimage(iv_book, book, iconname); }}} else { loadimage(iv_book, book, iconname);}iconCache.put(iconname,new SoftReference<Bitmap>(bitmap));