SoftReference in Java
That is, the soft reference to the 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.
The SoftReference feature is that an instance of it holds a soft reference to a Java object, and the existence of the soft reference does not prevent the garbage collection thread 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 recycles the Java object. Also, once a garbage thread reclaims the Java object, the Get () method returns null
Caching soft-referenced bitmap objects with the map collection
map<string, softreference<bitmap>> imagecache = new New hashmap<string, softreference<bitmap> > ();
Strongly-referenced bitmap objects
Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);
Soft-referenced bitmap objects
softreference<bitmap> Bitmapcache = new softreference<bitmap> (BITMAP);
Add the object to the map so that it is cached
Imagecache.put ("1", softrbitmap);
..
.
To take a soft-referenced bitmap object from the cache
softreference<bitmap> bitmapcache_ = Imagecache.get ("1");
Remove the Bitmap object, if the bitmap is recycled due to insufficient memory, it will get empty
Bitmap BITMAP_ = Bitmapcache_.get ();
If you need to load a large number of images from the Internet at this point, consider using the sdcard to create a temporary folder cache these pictures.
package com.minimax.softreferencedemo;import java.lang.ref.softreference;import java.util.hashmap;import java.util.map;import android.os.bundle;import android.app.activity; import android.graphics.bitmap;import android.graphics.bitmapfactory;import android.view.menu; import android.widget.imageview;public class mainactivity extends activity {@ Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); map<string, softreference<bitmap>> imagecache=new hashmap<string, Softreference<bitmap>> (); Bitmap bitmap=bitmapfactory.decoderesource (Getresources (), r.drawable.aidegenyuan); Softreference<bitmap> value=new softreference<bitmap> (Bitmap); ImageCache.put (" Yongyuanzaiyiqi ", value);//Use Imagecachesoftreference<bitmap> soft=imagecache.get (" YoNgyuanzaiyiqi "); Bitmap bitmap2=soft.get (); imageview imageview= (ImageView) findviewbyid (R.id.imageview); Imageview.setimagebitmap (BITMAP2);} @Overridepublic boolean oncreateoptionsmenu (Menu menu) {// inflate the menu ; this adds items to the action bar if it is Present.getmenuinflater (). Inflate (r.menu.main, menu); return true;}}
This article is from "Wish You Happiness" blog, please be sure to keep this source http://zhunixingfu.blog.51cto.com/7430537/1636250
The use of Android soft apps