Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Android memory optimization of the memory cache     - Achieve camera preview by ffplay (Linux)

- Three binary tree traversal (recursive, non-recursive traversal and Morris) (Programming)

- Manage SQL Server services login (start) account and password (Database)

- The hashcode method of Java (Programming)

- Docker deployment practices in Ubuntu (Server)

- Linux installed and tested the deployment of Kafka distributed cluster (Server)

- Java framework for parallel study - ForkJoin (Programming)

- Proficient in C / C ++ can also find a very good job (Programming)

- MySQL rollback insert statement causes ibd file grows double (Database)

- Android Qemu GPS module (Programming)

- Java deserialization test (Programming)

- Android Application Development: an argument between Activity (Programming)

- Linux text processing tool of sed (Linux)

- Fedora 22 install Adobe Flash Player (Linux)

- Configuring a Linux operating system against syn attack (Linux)

- Talk Packages (Linux)

- How to add and delete bookmarks in Ubuntu (Linux)

- MySQL + Corosync + Pacemaker + DRBD build highly available MySQL (Server)

- Linux System Tutorial: How to browse the Linux command line, weather forecast (Linux)

- Linux vi command list (Linux)

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

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) {
    @Override
      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.
     
         
         
         
  More:      
 
- Forbid screen change the window size when creating a new window under CentOS (Linux)
- Use libpq under Ubuntu 14.04 (Linux)
- Execute command sentence can result in equipment permanently bricked in Linux laptop (Linux)
- Linux / Unix system-level IO (Linux)
- CentOS 6.4 dial-up Raiders (Linux)
- The method of Linux into the rescue mode (Linux)
- Go powerful development server simple example (Server)
- Ubuntu 15.04 and CentOS 7 to deploy .NET, Mono and DNX (Server)
- Java Cookie Comments (Programming)
- Linux security configuration (Linux)
- CKEditor + SWFUpload achieve a more powerful editor (Linux)
- Eclipse 3.7.2 can not start solving under Ubuntu 14.04 (Linux)
- Protobuf compiled and used on the Ubuntu 14.04 (Programming)
- Archive log file size and redo log file size relationship (Database)
- How to cool down your Ubuntu system (Linux)
- Gitblit adopted Ticket collaborative development model (Linux)
- JavaScript function definition mode (Programming)
- Redhat 5 prohibit IPv6 (Linux)
- Installation Flow N Play- interface stylized video player on Ubuntu (Linux)
- Linux System Getting Started Learning: Join cron job in Linux (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.