A brief introduction to the following soft references:
SoftReference in Java is a soft reference to an object. If an object has soft references, enough memory space, the garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches. Using soft references can prevent memory leaks and enhance the robustness of the program.
Cache a soft-referenced bitmap object with the map collection:
Cache, used to store the background picture resource public map<string, softreference<bitmap>> mimagecachemap = new hashmap<string, Softreference<bitmap>> (); Bitmap Bitmap = Bitmapfactory.decodestream (InputStream, null,options); Softreference<bitmap> d = new softreference<bitmap> (BITMAP); Mimagecachemap.put (ImagePath, D);
To take a soft-referenced bitmap object from the cache:
softreference<bitmap> softreference = Mimagecachemap.get (IMAGEURL); if (softreference.get () = null) { return Softreference.get ();}
Note: If the bitmap object is removed, it will get empty if it is reclaimed due to insufficient memory bitmap.
Using soft reference cache bitmap