What is a cache?
The principle of caching technology is to treat all objects accessed by the user as a complete set, which is an algorithm that marks the objects that the user accesses frequently, and puts the objects into a set, which is a subset of the complete works. The next time the user accesses the object, it will first look up the subset of objects that the user wants to access, and then return to the object if they find it, and then find it in the complete set if it is not found. Of course, what I'm talking about here is the original reason, the cache is a lot of algorithms, and there are more than one level of cache, here is more than speak.
Why use a cache?
If you have a cache, you can save time and traffic by not having to read the files from the source address every time. In particular, mobile devices, frequent access to network resources will consume a lot of users of traffic and power, which is not tolerated by users, so regardless of which aspect of the application must be added to the cache.
Android What are the picture caches in? What are the characteristics of each?
the image cache of Android device is divided into two kinds, one is memory cache, picture is in memory of device, one is external cache, picture is cached on disk, disk can be internal storage space or external SD card. These two caches each have each advantage, the memory cache advantage is fast, the disadvantage is because also reads into the memory so also will consume the memory, therefore cannot be too big, uses the time to consider allocates the space, also has one disadvantage is after the application restarts disappears. The advantage of an external cache is that it can hold large amounts of data for long periods of time ( compared to memory caches ), and the downside is slow.
Memory Cache:
in the LruCache is recommended as a memory cache in Android, LruCache is actually a linkedhashmap ( supplemental Knowledge: Linkedhashmap is a two-way loop list that does not support thread safety,LruCache adds a thread-safe operation to it , and holds a certain number of object strong references, each time a new object is added to the header of the linked list. , when the allocated space is exhausted, the end object is removed, and the removed object can be recycled by GC . Here you need to pay attention to the capacity of LruCache, this capacity can not be too large, will cause OOM, not too small, do not play the role of caching. Google's website gives a view as a reference:
• Distribution LruCache size, consider how much memory your app has left.
• How many images are displayed on a single screen, and how many images are cached for display;
• Consider your phone's resolution and size, cache the same number of images,the larger the DPI of the phone will require more memory;
• Image resolution and pixel quality also determine the size of the memory occupied;
• How often do images are accessed, are there some images that are frequently accessed? If present you can consider using multiple · LruCache To do the cache, according to the frequency of access to different LruCache ;
• How to balance a piece of quality and quantity, sometimes you can consider caching low-resolution images, when used to request higher quality images in the background;
• In short you assign LruCache size can not be too big, but not too small, specific to the application also you need to consider comprehensively.
The following code is used examples of LruCache:
1 private lrucache<string, bitmap> Mmemorycache; // declaring cache space
2 Final int maxmemory = (int) (Runtime.getruntime (). MaxMemory ()/1024); // get the maximum memory allocation for an app in the system
3 // allocate 1/8 of application memory as cache space
4 final int cacheSize = MAXMEMORY/8;
5 Mmemorycache = new lrucache<string, bitmap> (cacheSize) {
6 @Override
7 protected int sizeOf (String key, Bitmap Bitmap) {
8 // rewrite the sizeOf method, return the number of bytes occupied by the picture instead of the number of pictures, each time the picture is added will be called
9 return Bitmap.getbytecount ()/1024;
10}
11};
Note: Some students may ask the following code:
1 intcachesize=4*1024*1024;//4MiB
2 Lrucachebitmapcache=newlrucache (cacheSize) {
3 protectedintsizeof (Stringkey,bitmapvalue) {
4 Returnvalue.getbytecount ();
5}
6}
The calculations for these two sizeOf are not the same, as explained here, the purpose of this method is to return the image to occupy the cache space instead of the number of pictures, and this value is the same unit as CacheSize .
Summarize:
combined with the above explanation, the use of memory cache LruCache When you need to know the following knowledge:
The LruCache encapsulates the Linkedhashmap, providing the ability to cache the LRU (Least Recently used least recently used algorithm );
LruCache automatically deletes the most recently accessed key-value pairs by means of the TrimToSize method;
LruCache does not allow null key values, linkedhashmap allowed;
LruCache thread Security, linkedhashmap thread is unsafe;
Inheritance LruCache , the SizeOf method must be used to calculate the size of each entry. safesizeof (k key, V value) will be called when put and get,safesizeof (k key, V value) will be called SizeOf (K key, V value), this method returns 1 by default .
A third-party beta tool is also recommended for APP for all aspects of detection:http://www.ineice.com/
Android Memory optimized memory cache