Approaching the end of 9102, BAT advanced development 21 Summary: Bitmap + Handler + performance optimization articles

Foreword

Interview: If you do not interview well-prepared, complete waste of time, it is not responsible for their own .

Today to share with you my finishing BAT interview topics and answers architecture, most of which are large enterprises face interview frequently asked questions, you can control this leak filled, of course, and certainly listed here can not cover all the way, but also I am hoping to play some help to find a job soon friends! As many in this text, I summed up the Android interview involved the scope and structure of frequently asked interview topics and answers parsing and architecture video for everyone to share information
Approaching the end of 9102, BAT advanced development 21 Summary: Bitmap + Handler + performance optimization articles
more complete project download. To be continued. Source. Graphic knowledge subsequent upload github. )
Can click on my contact me for a complete PDF or exchange
( VX: mm14525201314 )

Today, first to analyze Bitmap + Handler + Performance Optimization

A .Bitmap

1, Bitmap use what issues need attention?

Reference answer:

1) To select the appropriate picture size (bitmap type): Normally we optimize Bitmap, when you need to do performance tuning or to prevent OOM, we usually use RGB_565, because ALPHA_8 only transparency, shows the general picture does not make sense, Bitmap.Config.ARGB_4444show the picture is not clear, Bitmap.Config.ARGB_8888occupancy Up to memory. :

  • ALPHA_8 each pixel occupies 1byte memory
  • ARGB_4444 each pixel occupies 2byte memory
  • ARGB_8888 each pixel occupies 4byte memory (default)
  • RGB_565 each pixel occupies 2byte memory

2)降低采样率: BitmapFactory.Options 参数 inSampleSize 的使用,先把 options.inJustDecodeBounds 设为 true,只是去读取图片的大小,在拿到图片的大小之后和要显示的大小做比较通过 calculateInSampleSize()函数计算 inSampleSize 的具体值,得到值之后。options.inJustDecodeBounds 设为 false读图片资源。

3)复用内存: 即通过软引用(内存不够的时候才会回收掉),复用内存块,不需要再重新给这个 bitmap 申请一块新的内存,避免了一次内存的分配和回收,从而改善了运行效率。

4)使用 recycle()方法及时回收内存。

5)压缩图片

2、Bitmap.recycle()会立即回收么?什么时候会回收?如果没有地方使用这个 Bitmap,为什么垃圾回收不会直接回收?

参考回答:

通过源码可以了解到,加载 Bitmap 到内存里以后,是包含两部分内存区域的。简单的说,一部分是 Java 部分的,一部分是 C部分的。这个 Bitmap 对象是由 Java 部分分配的,不用的时候系统就会自动回收了

但是那个对应的 C 可用的内存区域,虚拟机是不能直接回收的,这个只能调用底层的功能释放。所以需要调用 recycle()方法来释放 C 部分的内存

bitmap.recycle()方法用于回收该 Bitmap 所占用的内存,接着将 bitmap 置空,最后使用 System.gc()调用一下系统的垃圾回收器进行回收,调用 System.gc()并不能保证立即开始进行回收过程,而只是为了加快回收的到来

3、一张 Bitmap 所占内存以及内存占用的计算

参考回答:

Bitamp 所占内存大小 = 宽度像素 x (inTargetDensity /inDensity) x 高度像素 x (inTargetDensity / inDensity)x 一个像素所占的内存字节大小

注: 这里 inDensity 表示目标图片的 dpi(放在哪个资源文件夹下),inTargetDensity 表示目标屏幕的 dpi,所以你可以发现 inDensityinTargetDensity 会对Bitmap 的宽高进行拉伸,进而改变 Bitmap 占用内存的大小。

在 Bitmap 里有两个获取内存占用大小的方法。

  • getByteCount()API12 加入,代表存储 Bitmap 的像素需要的最少内存。
  • getAllocationByteCount()API19 加入,代表在内存中为 Bitmap 分配的内存大小,代替了getByteCount() 方法。
  • 在不复用 Bitmap 时,getByteCount()getAllocationByteCount 返回的结果是一样的。在通过复用 Bitmap 来解码图片时,那么 getByteCount()表示新解码图片占用内存的大 小,getAllocationByteCount() 表示被复用 Bitmap 真实占用的内存大小

4、Android 中缓存更新策略 ?

参考回答:

Android 的缓存更新策略没有统一的标准,一般来说,缓存策略主要包含缓存的添加、获取和删除这三类操作,但不管是内存缓存还是存储设备缓存,它们的缓存容量是有限制的,因此删除一些旧缓存并添加新缓存,如何定义缓存的新旧这就是一种策略,不同的策略就对应着不同的缓存算法

比如可以简单地根据文件的最后修改时间来定义缓存的新旧,当缓存满时就将最后修改时间较早的缓存移除,这就是一种缓存算法,但不算很完美

5、LRU 的原理 ?

参考回答:

为减少流量消耗,可采用缓存策略。常用的缓存算法是LRU(Least Recently Used):当缓存满时, 会优先淘汰那些近期最少使用的缓存对象。主要是两种方式:

  • LruCache(内存缓存):LruCache 类是一个线程安全的泛型类:内部采用一个 LinkedHashMap 以强引用的方式存储外界的缓存对象,并提供 get 和 put 方法来完成缓存的获取和添加操作,当缓存满时会移除较早使用的缓存对象,再添加新的缓存对象。
  • DiskLruCache(磁盘缓存): 通过将缓存对象写入文件系统从而实现缓存效果

二.Handler

6.谈谈消息机制 Handler 作用 ?有哪些要素 ?流程是怎样的 ?

7.一个线程能否创建多个 Handler,Handler 跟 Looper 之间的对应关系 ?

8.软引用跟弱引用的区别

9.Handler 引起的内存泄露原因以及最佳解决方案

10.为什么系统不建议在子线程访问 UI?

11.Looper 死循环为什么不会导致应用卡死?

12.使用 Handler 的 postDealy 后消息队列会有什么变化?

13.可以在子线程直接 new 一个 Handler 吗?怎么做?

14.Message 可以如何创建?哪种效果更好,为什么?

三.性能优化

15.图片的三级缓存中,图片加载到内存中,如果内存快爆了,会发生什么?怎么处理?

16.内存中如果加载一张 500*500 的 png 高清图片.应该是占用多少的内存?

17.WebView 的性能优化 ?

18.Bitmap 如何处理大图,如一张 30M 的大图,如何预防 OOM?

19.内存回收机制与 GC 算法(各种算法的优缺点以及应用场景);GC 原理时机以及 GC 对象

20.内存泄露和内存溢出的区别 ?AS 有什么工具可以检测内存泄露

21.性能优化,怎么保证应用启动不卡顿? 黑白屏怎么处理?

最后

In order not to affect the reading, to the above interview questions for me summed up the Android Internet companies involved in the vast majority of programmers face interview questions and answers analysis and knowledge made the reference document (983 PDF) and architecture video for free for everyone to share! (Containing from basic to advanced. Containing BATJ. Byte beating interview topics, topics algorithms, high-end technology topics, mixed-use development themes, java interview topics, Android, Java little knowledge, to performance optimization. .View.OpenCV.NDK threads , etc. everything. there supplemented by related video + study notes )

Finishing is not easy, I hope you have a good success in the next year, sharing is a virtue, that share to more friends after a good friend thumbs up!

See the full PDF version
(.... To be continued more complete project source code download graphic knowledge subsequent upload github)
can click on my contact me for a full PDF
( VX: mm14525201314 )

Guess you like

Origin blog.51cto.com/14541311/2455779