面试题十一:异常与性能优化1(anr、oom、Bitmap),2024年最新面试要讲什么内容

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
img

正文

以上三者,内存溢出最需要注意、内存泄漏次之,内存抖动最后。

3、如何解决oom


3.1有关bitmap

图片显示:加载合适大小的图片,不要总是请求网络大图,有时可以使用缩略图。

及时释放内存

图片压缩

捕获异常

3.2其他方面

listview:convertview/lru

避免在onDraw方法里面执行对象的创建(如果在onDraw方法里面频繁地进行对象的创建,会造成内存突然上升,而在释放这些内存的时候,又会造成频繁地GC,这样就导致内存抖动)

谨慎使用多进程

三、Bitmap

========

1、recycle


public final class Bitmap implements Parcelable {

/**

  • Free the native object associated with this bitmap, and clear the

  • reference to the pixel data. This will not free the pixel data synchronously;

  • it simply allows it to be garbage collected if there are no other references.

  • The bitmap is marked as “dead”, meaning it will throw an exception if

  • getPixels() or setPixels() is called, and will draw nothing. This operation

  • cannot be reversed, so it should only be called if you are sure there are no

  • further uses for the bitmap. This is an advanced call, and normally need

  • not be called, since the normal GC process will free up this memory when

  • there are no more references to this bitmap.

*/

public void recycle() {

if (!mRecycled && mNativePtr != 0) {

if (nativeRecycle(mNativePtr)) {

// return value indicates whether native pixel object was actually recycled.

// false indicates that it is still in use at the native level and these

// objects should not be collected now. They will be collected later when the

// Bitmap itself is collected.

mNinePatchChunk = null;

}

mRecycled = true;

}

}

}

在源码中的注释:

该方法用于释放跟这个bitmap(位图)有关的native(本地)对象,并清除跟数据对象有关的reference(引用)。

但是这个方法并不会同步立即清除数据对象,系统会给垃圾回收机制(GC发送指令),如果GC发现这些数据没有

其他的引用的话,就会回收这些数据对象。

调用该方法后,该bitmap会被标记为"dead",因此任何调用它的方法都会报异常。

这个操作不可逆,所以一定要确保该位图没有被调用时,再谨慎地调用该方法。

该方法,一般不建议调用,因为改位图如果没有引用的情况下,会自动被GC回收。

2、LRU


package android.support.v4.util;

public class LruCache<K, V> {

private final LinkedHashMap<K, V> map;

public final V get(K key) {…}

public final V put(K key, V value) {…}

public void trimToSize(int maxSize) {…}

}

LruCache算法(Least Recently Used),也叫近期最少使用算法。它的主要算法原理就是把最近使用的对象用强引用存储在LinkedHashMap中,并且把最近最少使用的对象在缓存值达到预设定值之前从内存中移除。

相关文章:Android高效加载大图、多图解决方案,有效避免程序OOM

面试题:LRU算法是怎么实现的?

它的内部是用一个LinkedHashMap来实现的,里面提供了一个get、put方法来完成缓存的获取和添加。当缓存满的时候,它会提供一个trimToSize方法,把已添加的时间最长的缓存清除,添加新的缓存对象。

接下来我们看这个trimToSize方法:

/**

  • 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) {

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

throw new IllegalStateException(getClass().getName()

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

}

if (size <= maxSize || map.isEmpty()) {

break;

}

Map.Entry<K, V> toEvict = map.entrySet().iterator().next();

key = toEvict.getKey();

value = toEvict.getValue();

map.remove(key);

size -= safeSizeOf(key, value);

evictionCount++;

}

entryRemoved(true, key, value, null);

}

}

3、计算inSampleSize


// 根据maxWidth, maxHeight计算最合适的inSampleSize

public static int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

if (width > height) {

inSampleSize = Math.round((float)height / (float)reqHeight);

} else {

inSampleSize = Math.round((float)width / (float)reqWidth);

}

}

return inSampleSize;

}

4、缩略图


//缩略图

public static Bitmap thumbnail(String path,

int maxWidth, int maxHeight, boolean autoRotate) {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

// 获取这个图片的宽和高信息到options中, 此时返回bm为空

Bitmap bitmap = BitmapFactory.decodeFile(path, options);

options.inJustDecodeBounds = false;

// 计算缩放比

int sampleSize = calculateInSampleSize(options, maxWidth, maxHeight);

options.inSampleSize = sampleSize;

options.inPreferredConfig = Bitmap.Config.RGB_565;

options.inPurgeable = true;

options.inInputShareable = true;

if (bitmap != null && !bitmap.isRecycled()) {

bitmap.recycle();

}

bitmap = BitmapFactory.decodeFile(path, options);

return bitmap;

}

5、三级缓存


网络缓存、本地缓存、内存缓存

当我们的APP首次请求一张图片时,会从网络中获取,当这张图片加载成功之后,这张bitmap会被保存在内存和本地文件中各一份,当我们再次请求这张同样地址的图片时,就直接从内存或者本地文件中获取,避免再次从网络中获取,减少流量消耗。

6、BitmapUtils.java


public class BitmapUtils {

// 根据maxWidth, maxHeight计算最合适的inSampleSize

public static int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

if (width > height) {

inSampleSize = Math.round((float)height / (float)reqHeight);

} else {

inSampleSize = Math.round((float)width / (float)reqWidth);

}

}

return inSampleSize;

}

//缩略图

public static Bitmap thumbnail(String path,

int maxWidth, int maxHeight, boolean autoRotate) {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

尾声

如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

这里,笔者分享一份从架构哲学的层面来剖析的视频及资料给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

大厂面试真题

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

《2019-2021字节跳动Android面试历年真题解析》

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

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
img

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

)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-MQ7ehA6P-1713392459130)]

《2019-2021字节跳动Android面试历年真题解析》

[外链图片转存中…(img-ewRc0ENn-1713392459130)]

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

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-yKJ8ZnPu-1713392459131)]

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值