一、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 内存不再被引用了。