Using soft reference cache bitmap

Source: Internet
Author: User

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

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.