Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Android memory optimization of the memory cache     - Testing Oracle 11g RMAN replicate database (Database)

- Ubuntu will be written in a command file, executable file, source command (Linux)

- Configuring automatic mail GAMIT under CentOS system (Linux)

- Use Vagrant up a local development environment tutorials (Server)

- Vim useful plugin: vundle (Linux)

- Linux dd command applies amplification SWAP partition (Linux)

- SecureCRT 7.0 Log Ubuntu 12.04 server via SSH service under Vmware (Server)

- Graphing tool: Gnuplot (Linux)

- To create a problem by the statement date comparison of the difference between MySQL and Oracle (Database)

- learning Linux ls command examples (Linux)

- Use LKM change the default linux security level (Linux)

- Python: Finding meet the conditions specified in the file directory (Programming)

- LAN Deployment Docker-- from scratch to create your own private warehouse Docker (Linux)

- 6 common PHP security attacks (Linux)

- Based on OpenSSL for HTTPS service configuration (Server)

- Oracle Database import and export combat (Database)

- How to use the process on the desktop xkill end Linux (Linux)

- Oracle EBS R12 for Linux installation (Database)

- Java data structures - the linear form of the single-chain applications (Programming)

- Adjust the size of the hard disk VirtualBox (Linux)

  Android memory optimization of the memory cache
  Add Date : 2018-11-21      

The above two articles have talked about the basics and image loading method and optimization of images, all of which are optimized in order to avoid this problem the application appears OOM. A good application should not only robust but also can not go wrong user-friendly for the user of your application not only beautiful but also smooth, and soon presented to him wanted. In addition to optimizing load fast image loading outer required cache, the following article will tell image cache.

What is the cache?

Caching principle is that all the objects accessed by the user as a complete works, after marking algorithm which is the object of the user frequently accessed, these objects into a collection in this collection is a subset of the complete works, next time users access the this subset of the set will start to find the user wants to access an object if found directly back to this object, if it does not find complete works go look. Of course I am here to say is that the principle of things, there is a lot of caching algorithms, and some more than one cache, there is not much talk about it.

Why use the cache?

Cached, then you can do not always read the file from the source address, saving time also saves traffic. Especially mobile devices, frequent access to network resources users will consume a lot of traffic and power, this is the user can not stand, so no matter from which side application must be considered together with the cache.

Android in the image cache What? What are the characteristics?

Android device image cache are two, one is a memory cache, image cache in the device's memory, is an external cache, image cache on the disk, the disk can be internal storage space can be an external sd card. The merits of each of these two caches have, the advantage of fast cache memory, the disadvantage is also read into memory because it will consume memory, it is not too large, use the time to consider space allocation, there is a drawback it will disappear after a restart of the application. External cache advantage is that you can save a lot of long-term data (in terms of cache memory compared), the disadvantage is slow.

Memory cache:

In Android's official website recommended LruCache as a memory cache, LruCache actually a LinkedHashMap (additional knowledge: LinkedHashMap is a two-way circulation list, does not support thread safety, it encapsulates LruCache Added thread-safe operation), which holds certain strong reference number of objects, each new object is added in the head of the list, when allocated space runs out at the end of the object will be removed, the object can be removed gc recovered. It should be noted at LruCache capacity, this capacity is neither too big, it will cause OOM, but not too small, no effect of the cache. google official website gives their opinions as reference:

When the size of the allocation LruCache consider your application how much free memory;
A screen displays how many photos, how many pictures are cached and ready to be displayed;
Consider your cell phone resolution and size, same cache image number, dpi phones require larger memory will be greater, I have a blog to explain;
Image resolution and pixel quality also determines the size of the memory-intensive;
How often the picture is much visited, it is not there some pictures are frequently visited? If there is more than you can consider LruCache do cache, according to the frequency of access assigned to different LruCache in;
How to balance the image quality and quantity, in some cases it can be considered a low-resolution image cache, when used in the background and then request a higher quality image;
In short you assigned LruCache size neither too big and not too small, specific to the application you have to consider.

The following code is an example of using LruCache:

private LruCache < String, Bitmap> mMemoryCache; // declare the buffer space
final int maxMemory = (int) (Runtime.getRuntime () maxMemory () / 1024.); // Get the application in the system maximum memory allocation
1/8 // allocate memory as cache space applications
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache < String, Bitmap> (cacheSize) {
      protected int sizeOf (String key, Bitmap bitmap) {
            Number of occupied bytes // rewrite sizeOf method that returns the number of the picture and not the pictures, each picture is added will be called
          return bitmap.getByteCount () / 1024;

Note: Some students may ask the following code:

  int cacheSize = 4 * 1024 * 1024; // 4MiB
  LruCache bitmapCache = new LruCache (cacheSize) {
      protected int sizeOf (String key, Bitmap value) {
          return value.getByteCount ();

Both sizeOf calculation is not the same, explain here, this approach aims to rewrite return image and the number of occupied buffer space is not the picture, and the value of the unit and to cacheSize same.

to sum up:

Integrated the above explanation, in the use of the memory cache LruCache knowledge you need to know the following:

LruCache encapsulates LinkedHashMap, provides the LRU (Least Recently Used least recently used algorithm) cache function;
LruCache automatically delete the least recently accessed through trimToSize key method;
LruCache not allow null keys, LinkedHashMap allowed;
LruCache thread-safe, LinkedHashMap thread-safe;
Inheriting LruCache, it must duplicate sizeOf method for calculating the size of each entry. Put in the time and get calls safeSizeOf (K key, V value), safeSizeOf (K key, V value) calls sizeOf (K key, V value), this method returns 1 by default.
- C language Bubble Sort - integer sort (Programming)
- Android Studio installed in Ubuntu 15.04 (Linux)
- RabbitMQ installation, configuration, monitoring (Linux)
- Java Network Programming Internet address lookup (Programming)
- Yii PHP Framework Getting Started tutorial (Linux)
- Source code to compile and install MySQL 5.7.9 (Database)
- Linux System Getting Started Learning: the Linux Wireshark interface dead solve (Linux)
- JBPM6 Tutorial - taught you how to install JBPM (Linux)
- Nginx configuration support f4v video format player (Server)
- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)
- How to create a bootable USB disk to use MultiSystem on Ubuntu (Linux)
- About Auto Layout and frame (Programming)
- 10 useful Linux command Interview Questions and Answers (Linux)
- Build your own Web server under Ubuntu Linux system (Server)
- Linux remote wake the computer original code [C] (Linux)
- Use install_updates upgrade GAMIT / GLOBK (Linux)
- Build Docker based MongoDB replication cluster environment (Database)
- MySQL 5.7.9 source compiler installation instructions (Database)
- Tomcat installation under Linux (Server)
- Linux Hard Disk Partition and file system management (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.