Android based on softreference caching of pictures _android

Source: Internet
Author: User
Tags garbage collection

The example in this article describes how Android caches pictures based on SoftReference. Share to everyone for your reference, specific as follows:

SoftReference in Java is a soft reference to an 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

Bitmap object that caches soft references 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 ();

Note: If you need to load a large number of pictures from the Internet at this time, consider using the sdcard to create a temporary folder cache these pictures

For more information on Android-related content readers can view the site topics: "Android Cache Operation tips Summary", "Android development memory and caching skills Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

Related Article

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.