2024年安卓最全浅析LRUCache原理(Android),2024年最新2024历年阿里Android面试真题

最后

针对于上面的问题,我总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。
Android进阶视频+面试资料部分截图

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

// 当缓存的是图片的时候,这个size应该表示图片占用的内存的大小,所以应该重写里面调用的sizeOf(key, value)方法

size += safeSizeOf(key, value);

//向map中加入缓存对象,若缓存中已存在,返回已有的值,否则执行插入新的数据,并返回null

previous = map.put(key, value);

//如果已有缓存对象,则缓存大小恢复到之前

if (previous != null) {

size -= safeSizeOf(key, previous);

}

}

//entryRemoved()是个空方法,可以自行实现

if (previous != null) {

entryRemoved(false, key, previous, value);

}

trimToSize(maxSize);

return previous;

}

  • 开始的时候确实是把值放入LinkedHashMap,不管超不超过你设定的缓存容量。

  • 根据 safeSizeOf方法计算 此次添加数据的容量是多少,并且加到size 里 。

  • 方法执行到最后时,通过trimToSize()方法 来判断size 是否大于maxSize。

可以看到put()方法并没有太多的逻辑,重要的就是在添加过缓存对象后,调用 trimToSize()方法,来判断缓存是否已满,如果满了就要删除近期最少使用的数据。

2.trimToSize(int maxSize)

/**

  • Remove the eldest entries until the total of remaining entries is at or

  • below the requested size.

  • @param maxSize the maximum size of the cache before returning. May be -1

  •        to evict even 0-sized elements.
    

*/

public void trimToSize(int maxSize) {

while (true) {

K key;

V value;

synchronized (this) {

//如果map为空并且缓存size不等于0或者缓存size小于0,抛出异常

if (size < 0 || (map.isEmpty() && size != 0)) {

throw new IllegalStateException(getClass().getName()

  • “.sizeOf() is reporting inconsistent results!”);

}

//如果缓存大小size小于最大缓存,不需要再删除缓存对象,跳出循环

if (size <= maxSize) {

break;

}

//在缓存队列中查找最近最少使用的元素,若不存在,直接退出循环,若存在则直接在map中删除。

Map.Entry<K, V> toEvict = map.eldest();

if (toEvict == null) {

break;

}

key = toEvict.getKey();

value = toEvict.getValue();

map.remove(key);

size -= safeSizeOf(key, value);

//回收次数+1

evictionCount++;

}

entryRemoved(true, key, value, null);

}

}

/**

  • Returns the eldest entry in the map, or {@code null} if the map is empty.

  • Android-added.

  • @hide

*/

public Map.Entry<K, V> eldest() {

Entry<K, V> eldest = header.after;

return eldest != header ? eldest : null;

}

trimToSize()方法不断地删除LinkedHashMap中队首的元素,即近期最少访问的,直到缓存大小小于最大值。

3.LruCache.get(K key)

/**

  • Returns the value for {@code key} if it exists in the cache or can be

  • created by {@code #create}. If a value was returned, it is moved to the

  • head of the queue. This returns null if a value is not cached and cannot

  • be created.

  • 通过key获取缓存的数据,如果通过这个方法得到的需要的元素,那么这个元素会被放在缓存队列的尾部,

*/

public final V get(K key) {

if (key == null) {

throw new NullPointerException(“key == null”);

}

V mapValue;

synchronized (this) {

//从LinkedHashMap中获取数据。

mapValue = map.get(key);

if (mapValue != null) {

hitCount++;

return mapValue;

}

missCount++;

}

/*

  • 正常情况走不到下面

  • 因为默认的 create(K key) 逻辑为null

  • 走到这里的话说明实现了自定义的create(K key) 逻辑,比如返回了一个不为空的默认值

*/

/*

  • Attempt to create a value. This may take a long time, and the map

  • may be different when create() returns. If a conflicting value was

  • added to the map while create() was working, we leave that value in

  • the map and release the created value.

  • 译:如果通过key从缓存集合中获取不到缓存数据,就尝试使用creat(key)方法创造一个新数据。

  • create(key)默认返回的也是null,需要的时候可以重写这个方法。

*/

V createdValue = create(key);

if (createdValue == null) {

return null;

}

//如果重写了create(key)方法,创建了新的数据,就讲新数据放入缓存中。

synchronized (this) {

createCount++;

mapValue = map.put(key, createdValue);

if (mapValue != null) {

// There was a conflict so undo that last put

map.put(key, mapValue);

} else {

size += safeSizeOf(key, createdValue);

}

}

if (mapValue != null) {

entryRemoved(false, key, createdValue, mapValue);

return mapValue;

} else {

trimToSize(maxSize);

return createdValue;

}

}

最后

只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。

真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。

腾讯、字节跳动、阿里、百度等BAT大厂 2019-2021面试真题解析

资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

、阿里、百度等BAT大厂 2019-2021面试真题解析**

[外链图片转存中…(img-5WLOunHd-1715760137476)]

资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值