1. 内存管理基础知识

http://www.cnblogs.com/xingfuzzhd/p/3485924.html

  • 1. mImageView.setImageResource(R.drawable.my_image); 这段代码会调用 BitmapFactory.decodeStream() 生成一个 Bitmap。所以不要以为它比自己创建 Bitmap 节省内存。

3.  实际测试:

我使用了多种调用图片的方法来测试:

第一种:

// 直接载入资源 id
ImageView image = (ImageView) findViewById(R.id.imageView1)

第二种:

创建一个函数,根据 id 载入图片。杀死 Activity 时回收 Bitmap

public BitmapDrawable getImageBitmap(int idImage) {
  Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), idImage);
  images.add(bitmap);
  return new BitmapDrawable(context.getResources(), bitmap);
}

第三种:

创建一个类

/**
* solution for OOM ------> compress bitmap
*/
public class BitmapUtils {
private static final String TAG = "BitmapUtils";
/**
* calulate the compress rate
*/
private static int calculateInSampleSize(BitmapFactory.Options opts,int reqHeight,int reqWidth) {
if(opts == null) {
return -1;
}
int width = opts.outWidth;
int height = opts.outHeight; int sampleSize = 1; if(width > reqWidth || height > reqHeight) {
int heightRatio = (int) (height/(float)reqHeight);
int widthRatio = (int) (width/(float)reqWidth);
sampleSize = (heightRatio > widthRatio) ? widthRatio : heightRatio;
}
return sampleSize;
} /**
* compress an image refer to the goal dimension
* @param res
* @param resId
* @param reqWidth
* @param reqHeight
* @return Bitmap
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, opts);
int sampleSize = calculateInSampleSize(opts, reqHeight, reqWidth);
Log.i(TAG,"before[width:"+opts.outWidth+",height:"+opts.outHeight+"]");
opts.inJustDecodeBounds = false;
opts.inSampleSize = sampleSize;
/* newly added */
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inPurgeable = true;
opts.inInputShareable = true;
/* newly added */
Log.i(TAG,"insamplesize="+sampleSize);
Bitmap bitmap = BitmapFactory.decodeResource(res, resId, opts);
Log.i(TAG,"after[width:"+bitmap.getWidth()+",height:"+bitmap.getHeight()+"]");
return bitmap;
} /**
* compress an image refer to the goal dimension
*
* @param data image byte array
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap decodeSampledBitmapFromByteArray(byte[] data,int reqWidth,int reqHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Log.i(TAG,"before[width:"+opts.outWidth+",height:"+opts.outHeight+"]");
opts.inSampleSize = calculateInSampleSize(opts, reqHeight, reqWidth);
Log.i(TAG,"insamplesize="+opts.inSampleSize);
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Log.i(TAG,"after[width:"+bitmap.getWidth()+",height:"+bitmap.getHeight()+"]");
return bitmap;
} public static Bitmap decodeResourceWithLowMemory(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
// Bitmap.Config.ALPHA_8,Bitmap.Config.ARGB_4444,Bitmap.Config.RGB_565
// 设置这几个参数效果都差不多,确实相当省内存啊,而且还跟原图清晰度相当
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
return BitmapFactory.decodeResource(context.getResources(), resId, opt);
} public static Bitmap loadToImageViewFromResource(Resources res,int resId,ImageView imageView) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, opts);
int reqHeight = imageView.getLayoutParams().height;
int reqWidth = imageView.getLayoutParams().width;
int sampleSize = calculateInSampleSize(opts, reqHeight, reqWidth);
Log.i(TAG,"before[width:"+opts.outWidth+",height:"+opts.outHeight+"]");
opts.inJustDecodeBounds = false;
opts.inSampleSize = sampleSize;
/* newly added */
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inPurgeable = true;
opts.inInputShareable = true;
/* newly added */
Log.i(TAG,"insamplesize="+sampleSize);
Bitmap bitmap = BitmapFactory.decodeResource(res, resId, opts);
Log.i(TAG,"after[width:"+bitmap.getWidth()+",height:"+bitmap.getHeight()+"]");
return bitmap;
} }

4. 测试过程:

创建两个 Activity,每个都载入几个大图片,然后两个 Activity 之间不停的切换。

第一种方法,切换多次后 OOM (out of memory)。 第二种方法,更快的 OOM。

第三种,如果压缩率够高的话,几乎不会 OOM。比如原始图片为 2048 * 1600,然后目标 ImageView 的大小为 200* 150。最后内存消耗将大大减小。然后如果

两者分辨率差距不大,最后内存消耗将不会被优化,最后也将 OOM。

5. 原理

通常,我们使用BitmapFactory.decodeResource()方法来从资源文件中读取一张 图片并生成一个 Bitmap。但如果使用一个BitmapFactory.Options对象,并把该对象的inJustDecodeBounds属性设 置为true,decodeResource()方法就不会生成 Bitmap 对象,而仅仅是读取该图片的尺寸和类型信息。所以在前面的类的一些方法中,我们用 BitmapFactory decode了两次图片,但第一次只是获取信息而没有生成 Bitmap 对象,所以没有未回收的 Bitmap 出现。

6. 一些推荐的解决方案

弱引用,LruCache,或者开源项目 :Android-Universal-Image-Loader

延伸:一个Bitmap到底占用多大内存?系统给每个应用程序分配多大内存?

· Bitmap占用的内存为:像素总数 * 每个像素占用的内存。在Android中,Bitmap有四种像素类型:ARGB_8888、ARGB_4444、ARGB_565、ALPHA_8,他 们每个像素占用的字节数分别为4、2、2、1。因此,一个2000*1000的ARGB_8888类型的Bitmap占用的内存为 2000*1000*4=8000000B=8MB。

· Android根据设备屏幕尺寸和dpi的不同,给系统分配的单应用程序内存大小也不同,具体如下表(表格取自Android 4.4 Compatibility Definition Document (CDD)):

屏幕尺寸  DPI  应用内存
 small / normal / large  ldpi / mdpi  16MB
 small / normal / large  tvdpi / hdpi  32MB
 small / normal / large  xhdpi  64MB
 small / normal / large  400dpi  96MB
 small / normal / large  xxhdpi  128MB
 xlarge  mdpi  32MB
 xlarge  tvdpi / hdpi  64MB
 xlarge  xhdpi  128MB
 xlarge  400dpi  192MB
 xlarge  xxhdpi  256MB

Android 内存管理研究的更多相关文章

  1. Android内存管理机制之一:low memory killer

    转载自http://www.miui.com/thread-29268-1-1.html 准备写这个专题之前,心里是有点忐忑的.首先Android内存管理机制相当复杂,想要讲清楚比较困难:其次对于绝大 ...

  2. Android 内存管理分析(四)

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...

  3. 移动端测试===Android内存管理: 理解App的PSS

    Android内存管理: 理解App的PSS 原文链接:http://www.littleeye.co/blog/2013/06/11/android-memory-management-unders ...

  4. 浅谈Android内存管理

    最近在网上看了不少Android内存管理方面的博文,但是文章大多都是就单个方面去介绍内存管理,没有能全局把握,缺乏系统性阐述,而且有些观点有误,仅仅知道这些,还是无法从整体上理解内存管理,对培养系统优 ...

  5. [Android Memory] Android内存管理、监测剖析

    转载自:http://blog.csdn.net/anlegor/article/details/23398785 Android内存管理机制: Android内存管理主要有:LowMemory Ki ...

  6. Android——内存管理基础

    内存收集概念 内存垃圾收集器(garbage collector) 概念:自定内存管理. 功能:分配内存.保证所有被引用的对象还在内存中.可以释放在运行的代码中不再引用的对象的内存. 垃圾收集器避免了 ...

  7. Android内存管理

    首先Android理机制相当复杂.想要讲清楚比較困难.其次对于绝大多数用户来说.仅仅关心内存够不够用,至于内存怎样管理的这样的技术细节,不是用户须要去考虑的,写这样一个专题有没有意义?毕竟我们是用手机 ...

  8. Android 内存管理 &Memory Leak & OOM 分析

    转载博客:http://blog.csdn.net/vshuang/article/details/39647167 1.Android 进程管理&内存 Android主要应用在嵌入式设备当中 ...

  9. 【原创】Android内存管理-OnTrimMemory

    Application中有两个与内存管理相关的方法:onLowMemory()和 onTrimMemory(int level),源码如下 @CallSuper public void onLowMe ...

随机推荐

  1. 云计算与虚拟化KVM深度实践

    徐亮伟, 江湖人称标杆徐.多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作.擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作. 个人博客"徐亮伟架构师之路&quo ...

  2. TCP/IP协议 socket

    TCP/IP四层协议 TCP/IP概念 tcp/ip协议是主机接入互联网以及接入互联网的两台主机通信的标准. 数据帧概念 数据帧 |-- 包头 | |--源地址 | |--目标地址 | |--数据类型 ...

  3. springmvc怎么在启动时自己执行一个线程

    之前使用springmvc的时候,都是写好controller和对应的数据库操作. 外界发请求的时候,controller进行一堆操作后返回相应的json数据. 似乎springmvc就是外界驱动的一 ...

  4. localStorage,sessionStorage

    h5的本地存储. 现代浏览器普遍开始支持H5本地存储,localStorage.sessionStorage.可以用来代替cookie的一部分存储功能,他比cookie存储量更大.比较实用. 两者用法 ...

  5. angular性能优化心得

    原文出处 脏数据检查 != 轮询检查更新 谈起angular的脏检查机制(dirty-checking), 常见的误解就是认为: ng是定时轮询去检查model是否变更.其实,ng只有在指定事件触发后 ...

  6. unity平行光太亮?物体发白?可能你使用了2个或多个平行光

    unity平行光太亮?物体发白?可能你使用了2个或多个平行光 今天做项目时就遇到了这个问题,光亮得让物体发白 发现加载的场景 里面有个 平行光,删了就好了 要是感觉还是太亮,就把主平行光的Intens ...

  7. PEAR

    简介:pear是php扩展与应用库(the php extension and application repository)的缩写.它是一个php扩展及应用的一个代码仓库. 编码规范:参考(http ...

  8. 映射文件中增删改查标签中的parameterType和resultType

    parameterType:指定输入参数类型,mybatis通过ognl从输入对象中获取参数值拼接在sql中. resultType:指定输出结果类型,mybatis将sql查询结果的一行记录数据映射 ...

  9. 2.QWidget类

    简述: QWidget类是所有用户界面对象的基类. Widget是用户界面的基本单元:它从窗口系统接收鼠标,键盘和其他事件,并在屏幕上绘制自己. 每个Widget都是矩形的,它们按照Z-order进行 ...

  10. 企业招聘:UX设计师需要满足他们哪些期望?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   为了确定2017年最有价值的用户体验技能和特质,我特地参考了150多份工作要求.最后,得出了以下 ...