Android内存溢出

Android虽然会自动管理内存,JAVA也有garbage collection (GC )内存回收机制。
一、移动终端因为内存有限,往往图片处理经常出现上述的错误。解决方法:
1.明确调用System.gc(); 这种内存回收会有一定的作用,但是请不要太期待。
2.图片处理完成后回收内存。 请在调用BitMap进行图片处理后进行内存回收。 bitmap.recycle(); 这样会把刚刚用过的图片占用的内存释放。
3.图片处理时指定大小。
    public Bitmap getBitpMap() {
        ParcelFileDescriptor pfd;
        try {
            pfd = this.getContentResolver().openFileDescriptor(new URL("").toURI(), "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   
    public 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;
    }

二、解决加载图片内存溢出的问题

1.Options只保存图片尺寸大小,不保存图片到内存

BitmapFactory.Options opts = new BitmapFactory.Options();

2.缩放的比例,缩放是很难按准备的比例进行缩放的,其值表明缩放的倍数,SDK中建议其值是2的指数值,值越大会导致图片不清晰

opts.inSampleSize=4;
Bitmapbmp=null; 
bmp=BitmapFactory.decodeResource(getResources(),mImageIds[position],opts);

3.回收

bmp.recycle();

4.优化Dalvik虚拟机的堆内存分配

于Android平台来说,其托管层使用的DalvikJavaVM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应 用中可能考虑手动干涉GC处理,使用dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以 增强程序堆内存的处理效率。当然具体原理我们可以参考开源工程,这里我们仅说下使用方 法:

private final static float TARGET_HEAP_UTILIZATION=0.75f;

在程序onCreate时就可以调用

VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

5.堆内存自己定义大小

对于一些Android项目,影响性能瓶颈的主要是Android自己内存管理机制问题,除了优化Dalvik虚拟机的堆内存分配外,我们还可以强制定 义自己软件的对内存大小,我们使用Dalvik提供的dalvik.system.VMRuntime类来设置最小堆内存为例:
  

private final static int CWJ_HEAP_SIZE=6*1024*1024;
  
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
//----------------------------------------------------------------------
 private final static int CWJ_HEAP_SIZE= 6*1024*1024;  

 private final static float TARGET_HEAP_UTILIZATION = 0.75f;   

 VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);  

 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

 

//设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理

总结:

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

2.对图片采用软引用,及时地进行recyle()操作

3.合理设计编码

4.缩放图片比例,只保存图片尺寸大小,不把图片保存到内存

5.Android堆内存也可以自己定义大小和优化Dalvik虚拟机的内存

 

 

 

 

posted on 2013-05-19 00:21  洛易  阅读(236)  评论(0编辑  收藏  举报