Android加载大图片内存溢出的问题总结

   写Android代码也有那么三四个月的时间了,也一直都是菜鸟,很多东西都只是拿来就用,也没有想太多的细节问题。
   之前在程序中遇到图片资源的时候,总是使用BitmapFactory.decodeResource来设置图片资源,也没发现什么问题。前段时间在写一个动态壁纸程序的时候,可能图片资源比较多,毕竟手机内存还是有限的,程序跑起来有时会out of Memory异常。后来改用BitmapFactory.decodeStream方法,好像能解决问题。
    后来在程序中去测试对比了一下,发现还是有比较大的差别的。同样是加载十张图片,我们先看看使用BitmapFactory.decodeResource后的内存占用情况:
    //加载图片前的空余内存空间
    long freeStart = Runtime.getRuntime().freeMemory();
    bubble2 = BitmapFactory.decodeResource(resources, R.drawable.bubble2);
    bubble5 = BitmapFactory.decodeResource(resources, R.drawable.bubble5);
    bubble_2 = BitmapFactory.decodeResource(resources, R.drawable.bubble_2);
    speeding = BitmapFactory.decodeResource(resources, R.drawable.speeding);
    slowing = BitmapFactory.decodeResource(resources, R.drawable.slowing);
    resee = BitmapFactory.decodeResource(resources, R.drawable.resee);
    network = BitmapFactory.decodeResource(resources, R.drawable.network5);
    audio = BitmapFactory.decodeResource(resources, R.drawable.audio);
    eye_back = BitmapFactory.decodeResource(resources, R.drawable.eye_back);
    eye = BitmapFactory.decodeResource(resources, R.drawable.eye);
    //加载图片后的空余内存空间
    long freeEnd = Runtime.getRuntime().freeMemory();
  System.out.println("freeStart:"+freeStart+"nfreeEnd:"+freeEnd+"n 相差:"+(freeStart-freeEnd));
   运行结果是:
   [转载]Android加载大图片内存溢出的问题总结

    
    再来看看使用BitmapFactory.decodeStream的情况:
 
public Bitmap readBitmap(Context context, int id){
     BitmapFactory.Options opt = new BitmapFactory.Options();
     opt.inPreferredConfig=Bitmap.Config.RGB_565;//表示16位位图 565代表对应三原色占的位数
     opt.inInputShareable=true;
     opt.inPurgeable=true;//设置图片可以被回收
     InputStream is = context.getResources().openRawResource(id);
 
     return BitmapFactory.decodeStream(is, null, opt);
   
 
//加载图片前的空余内存空间
long freeStart = Runtime.getRuntime().freeMemory();
bubble2 = utils.readBitmap(context, R.drawable.bubble2);
bubble5 = utils.readBitmap(context, R.drawable.bubble5);
bubble_2 = utils.readBitmap(context, R.drawable.bubble_2);
speeding = utils.readBitmap(context, R.drawable.speeding);
slowing = utils.readBitmap(context, R.drawable.slowing);
resee = utils.readBitmap(context, R.drawable.resee);
network = utils.readBitmap(context, R.drawable.network5);
audio = utils.readBitmap(context, R.drawable.audio);
eye_back = utils.readBitmap(context, R.drawable.eye_back);
eye = utils.readBitmap(context, R.drawable.eye);
//加载图片后的空余内存空间
long freeEnd = Runtime.getRuntime().freeMemory();
System.out.println("freeStart:"+freeStart+"nfreeEnd:"+freeEnd+"n 相差:"+(freeStart-freeEnd));
 
        运行结果是:
        [转载]Android加载大图片内存溢出的问题总结
        
     从两个的运行结果中可以看出,使用 BitmapFactory.decodeResource 来设置图片资源要消耗更多的内存,如果程序中的图片资源很多的话,那这个内存就很客观啦。主要因为是 BitmapFactory.decodeResource 是通过Java层来createBitmap来完成图片的加载,增加了java层的内存消耗。而 BitmapFactory.decodeStream 则是直接调用了JNI,避免了java层的消耗。同时,在加载图片时,图片Config参数也可以有效减少内存的消耗。比如图片存储的位数及options.inSampleSize 图片的尺寸等。
     平时遇到的小小问题,总结一下,就写这么多吧。
原文地址:https://www.cnblogs.com/zhwl/p/2736553.html