Android 内存溢出问题分析。

来源:互联网 发布:amazo。it 编辑:程序博客网 时间:2024/05/17 22:28
 

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的图片时是必须的。

 

 

view plaincopy to clipboardprint?
  1. BitMap getBitpMap(){  
  2. ParcelFileDescriptor pfd;  
  3. try{  
  4.     pfd = mCon.getContentResolver().openFileDescriptor(uri, "r");  
  5. }catch (IOException ex){  
  6.     return null;  
  7. }  
  8. java.io.FileDescriptor fd = pfd.getFileDescriptor();  
  9. BitmapFactory.Options options = new BitmapFactory.Options();  
  10.     //先指定原始大小   
  11. options.inSampleSize = 1;  
  12.     //只进行大小判断   
  13.     options.inJustDecodeBounds = true;  
  14.     //调用此方法得到options得到图片的大小   
  15.     BitmapFactory.decodeFileDescriptor(fd, null, options);  
  16.     //我们的目标是在800pixel的画面上显示。   
  17.     //所以需要调用computeSampleSize得到图片缩放的比例   
  18.     options.inSampleSize = computeSampleSize(options, 800);  
  19.     //OK,我们得到了缩放的比例,现在开始正式读入BitMap数据   
  20.     options.inJustDecodeBounds = false;  
  21.     options.inDither = false;  
  22.     options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
  23.       
  24.     //根据options参数,减少所需要的内存   
  25.     Bitmap sourceBitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);  
  26.     return sourceBitmap;  
  27. }  
  28. //这个函数会对图片的大小进行判断,并得到合适的缩放比例,比如2即1/2,3即1/3   
  29. static int computeSampleSize(BitmapFactory.Options options, int target) {  
  30.     int w = options.outWidth;  
  31.     int h = options.outHeight;  
  32.     int candidateW = w / target;  
  33.     int candidateH = h / target;  
  34.     int candidate = Math.max(candidateW, candidateH);  
  35.     if (candidate == 0)  
  36.         return 1;  
  37.     if (candidate > 1) {  
  38.         if ((w > target) && (w / candidate) < target)  
  39.             candidate -= 1;  
  40.     }  
  41.     if (candidate > 1) {  
  42.         if ((h > target) && (h / candidate) < target)  
  43.             candidate -= 1;  
  44.     }  
  45.     if (VERBOSE)  
  46.         Log.v(TAG, "for w/h " + w + "/" + h + " returning " + candidate + "(" + (w/candidate) + " / " + (h/candidate));  
  47.     return candidate;  
  48. }