android bitmap 内存使用分析:

一、image bitmap内存分配:

1、  android 2.3.3(API level 10) and lower:

Bitmap pixel data 存储在native memory,需要调用Bitmap.recycle()方法进行内存回收。

 

2、  android 3.0(API level 11) and Higher:

Bitmap pixel data 存储在Dalvik heap。

 

参考文档:

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

 

 

二、image bitmap内存释放(android 4.2):

参考:http://lab.tigerpenguin.com/2014/03/android-bitmap-memory-analysis-part-3.html

 

写一个简单的test code.

1、
@Override

2、 protectedvoidonCreate(BundlesavedInstanceState) {

3、     super.onStart();

4、    //setting image resource from drawable

5、   imageView = (ImageView)findViewById(R.id.imageView1);

6、    BitmapFactory.Options opts = newBitmapFactory.Options();

7、    opts.inJustDecodeBounds = false;

8、     bmp1 =BitmapFactory.decodeFile("/data/1.jpeg", opts);

9、 imageView.setImageBitmap(bmp1);

10、  }

11、   

12、  @Override

13、  protectedvoidonStop() {

14、      super.onStop();

15、     if(!bmp1.isRecycled() )

16、     {

17、                   bmp1.recycle() ;

18、                   imageView.setImageBitmap(null);

19、                   imageView = null;

20、                   bmp1 = null;

21、       }

22、  }

 

 



 

退出程序后,执行了onStop() 函数,但是bitmap 内存还是被引用,从以下的图看出是被GLES20DisplayList 引用,这可能是android 4.2的一个bug(http://stackoverflow.com/questions/13754876/release-bitmaps-from-android-view-gles20displaylist),我们可以disable硬件解码功能来规避这个问题。

 

我们可以通过以下方法了disable 硬件解码功能:

在onCreate() 函数中加上

imageView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

不用硬件解码功能后,可以看出,apk退出后bitmap 内存不再被引用了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值