Specific explanation android Picture download frame Universialimageloader memory Cache (iii)

Source: Internet
Author: User

The previous two articles focus on disk caching, which is mainly about memory caching. For the memory cache. Also intends to be divided into two articles to explain. In this article, we mainly focus on three classes,

MemoryCache, Basememorycache and Limitedmemorycache.

First, let's look at the memory cache interface MemoryCache.

Put (String key, Bitmap value); Bitmap get (String key); Bitmap Remove (String key); collection<string> keys (); void clear ();

As can be seen from the above, the overall design of the interface is divided into 5 methods. 1, into the real bitmap 2, through the key to get bitmap 3, by key Delete bitmap 4, iterate to get all the keys of the set 5, empty the memory cache.


Next we look at the abstract class Basememorycache of the interface that implements the memory cache.

As with the previous article, start with the variable.

/** Stores not strong references to objects */private final map<string, reference<bitmap>> softmap = Collectio Ns.synchronizedmap (New hashmap<string, reference<bitmap>> ());

As is said. This variable is an object that stores non-strong references.

A little bit of attention to the following methods

@Overridepublic Bitmap get (String key) {Bitmap result = null; reference<bitmap> Reference = Softmap.get (key), if (Reference! = null) {result = Reference.get ();} return result;}

Gets the numeric bitmap in the soft reference by the key.


Finally, let's look at the cache class Limitedmemorycache of the limited memory cache space, from the inheritance relationship. It is a further extension of the Basememorycache.

From the variable point of view:

private static final int max_normal_cache_size_in_mb = 16;private static final int max_normal_cache_size = Max_normal_cach E_SIZE_IN_MB * 1024x768 * 1024;private final int sizelimit;private final Atomicinteger cachesize;private final list<bitmap& Gt Hardcache = collections.synchronizedlist (New linkedlist<bitmap> ());

From the definition of a variable, contains the maximum cache limit, the size of the current cache, and a collection of strongly referenced objects.

Take the storage of the objects in the method to analyze:

Public Boolean put (String key, Bitmap value) {Boolean putsuccessfully = false;//Try To add value to hard Cacheint Valuesi  Ze = getsize (value), int sizeLimit = Getsizelimit (); int curcachesize = Cachesize.get (); if (Valuesize < SizeLimit) {while (Curcachesize + valuesize > SizeLimit) {Bitmap Removedvalue = Removenext (), if (Hardcache.remove (Removedvalue)) {curcachesize = Cachesize.addandget (-getSize ( Removedvalue));}} Hardcache.add (value); Cachesize.addandget (valuesize);p utsuccessfully = true;} Add value to soft cachesuper.put (key, value); return putsuccessfully;}

It's very obvious that when the picture is cached. First, it is necessary to infer that the increase in the current picture has no limit over the size of the overall cached memory. Assumptions exceeded. The first is based on a different strategy. Delete the first need to delete the picture, assuming the appropriate, the current picture inserted, assuming inappropriate. Continue the iteration.


Ok, the first article about the memory cache of a picture is here, and it will continue to be analyzed later. Hope to have some help for you oh ~




Specific explanation android picture download frame Universialimageloader memory cache (c)

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.