面试题十一:异常与性能优化1(anr、oom、Bitmap)

内存抖动:短时间内大量对象被创建,然后又被释放,这种情况如果达到内存的阈值会触发GC

内存泄漏:一些不再使用的垃圾对象,由于引用了还在使用的对象,导致不能被回收,这些垃圾对象多了,就会导致oom。

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

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 indic

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值