Detailed explanation of Android memory optimization — lrucache
Concept:
LruCache
What is lrucache?
What is the implementation principle of lrucache?
In fact, these two questions can be answered as one question. When you know what lrucache is, you only know the implementation principle of lrucache; LRU's full name is least recently used! Therefore, we can infer the implementation principle of lrucache: remove the least recently used data from the cache and retain the most frequently used data. How to implement the specific code? Let's go to the source code.
Lrucache source code analysis
Lrucache usage
Let's take a look at two diagrams of memory usage
Figure-1
Figure-2
The above memory analysis diagram analyzes the data of the same application. The only difference is that figure-1 does not use lrucache, while figure-2 uses lrucache; It is obvious that the memory usage in figure-1 is obviously too large, basically about 30m, while the memory usage in figure-2 is basically about 20m. This saves nearly 10m of memory!
OK, let's post the implementation code
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!