Android LRUCache Principle

Source: Internet
Author: User
LRUCache principle:

Maintain a list of cached objects and arrange objects in the order they are accessed, that is, objects that have not been accessed are placed at the end of the queue and are about to be eliminated. The most recently visited objects were put on the team head and finally eliminated. And this queue is mainly implemented by Linkedhashmap.

Linkedhashmap: (The default is insert sort, i.e. the input is consistent with the output order)

Inherit from HashMap, using a doubly linked list to store entry order relationships in the map. The order has 2, one is the access order, the first is the insertion order, it can be constructed by its method linkedhashmap (int initialcapacity,float Loadfactor,boolean accessorder:true to denote sequential access, False is the insertion sort) specified. So, get,put,remove and so on, in addition to doing hashmap operation, but also need to adjust entry order list of work.

LRUCache in Android sets the default order in Linkedhashmap to the order of access, each time a get is called, the object is moved to the head of the linked list, and a put is called to insert a new object into the list header. When the memory cache reaches the maximum value, the object at the tail of the list is removed. Each time you put or remove, you need to determine whether the cache size is trimtosize enough.

Note: The list header-recently used or inserted, the tail of the list-rarely used recently.

This picture comes from the network, if there is infringement please contact me, will be deleted!

Android LRUCache Principle

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.