Android 内存溢出问题分析

Android虽然会自动管理内存,JAVA也有garbage collection (GC )内存回收机制。

但是如果程序在一次操作中打开几个M的文件,那么通常会出现下面的错误信息。

02-04 21:46:08.703: ERROR/dalvikvm-heap(2429): 1920000-byte external allocation too large for this process.

02-04 21:52:28.463: ERROR/AndroidRuntime(2429): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

移动终端因为内存有限,往往图片处理经常出现上述的错误。

解决方法:

1.明确调用System.gc();

   这种内存回收会有一定的作用,但是请不要太期待。

2.图片处理完成后回收内存。

  请在调用BitMap进行图片处理后进行内存回收。

  bitmap.recycle();

  这样会把刚刚用过的图片占用的内存释放。

3.图片处理时指定大小。

  下面这个方法处理几个M的图片时是必须的。

      BitMap getBitpMap(){
    ParcelFileDescriptor pfd;
    try{
     pfd = mCon.getContentResolver().openFileDescriptor(uri, "r");
    }catch (IOException ex){
     return null;
    }
    java.io.FileDescriptor fd = pfd.getFileDescriptor();
    BitmapFactory.Options options = new BitmapFactory.Options();
        //先指定原始大小
    options.inSampleSize = 1;
        //只进行大小判断
        options.inJustDecodeBounds = true;
        //调用此方法得到options得到图片的大小
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        //我们的目标是在800pixel的画面上显示。
        //所以需要调用computeSampleSize得到图片缩放的比例
        options.inSampleSize = computeSampleSize(options, 800);
        //OK,我们得到了缩放的比例,现在开始正式读入BitMap数据
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
       
        //根据options参数,减少所需要的内存
        Bitmap sourceBitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
        return sourceBitmap;
    }
    //这个函数会对图片的大小进行判断,并得到合适的缩放比例,比如2即1/2,3即1/3
    static int computeSampleSize(BitmapFactory.Options options, int target) {
        int w = options.outWidth;
        int h = options.outHeight;
        int candidateW = w / target;
        int candidateH = h / target;
        int candidate = Math.max(candidateW, candidateH);
        if (candidate == 0)
            return 1;
        if (candidate > 1) {
            if ((w > target) && (w / candidate) < target)
                candidate -= 1;
        }
        if (candidate > 1) {
            if ((h > target) && (h / candidate) < target)
                candidate -= 1;
        }
        if (VERBOSE)
            Log.v(TAG, "for w/h " + w + "/" + h + " returning " + candidate + "(" + (w/candidate) + " / " + (h/candidate));
        return candidate;
    }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gieghia007/archive/2010/07/09/5723996.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值