【问题标题】:Grow Heap size and OutOfMemory error增加堆大小和 OutOfMemory 错误
【发布时间】:2013-03-09 02:51:52
【问题描述】:

我有一张大约 1948X674 尺寸和 104kb 大小的图像。它在三星 Galaxy S3 上出现 OutOfMemoryError。我在水平滚动视图中显示此图像。 这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Panorama" >

    <HorizontalScrollView
        android:id="@+id/hscroll"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_above="@+id/tapToEnter"
        android:scrollbars="none" >

        <ImageView
            android:id="@+id/imgPanorama"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </HorizontalScrollView>

    <Button
        android:id="@+id/tapToEnter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Tap to enter" />

</RelativeLayout>

我正在以编程方式为其设置图像。 decodeSampledBitmapFromResource() 我正在使用来自 Loading Large Bitmap Efficiently

Bitmap bmp;
try
        {
        BitmapDrawable bd=(BitmapDrawable)getResources().getDrawable(R.drawable.panorama);
        reqwidth=bd.getIntrinsicWidth();
        reqheight = bd.getIntrinsicHeight();
        }catch(OutOfMemoryError oom)
        {
            oom.printStackTrace();
        }
bmp=decodeSampledBitmapFromResource(getResources(),R.drawable.panorama, reqwidth, reqheight);
imgPanorama.setImageBitmap(bmp);




public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;


        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable=true;
        /*options.inDither=false;                     //Disable Dithering mode
        options.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        options.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        options.inTempStorage=new byte[32 * 1024];
        */
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }


@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(bmp!=null  && !bmp.isRecycled())
        {
            bmp.recycle();
            bmp=null;

        }
    }

这是我开始活动时的日志。

03-20 11:20:36.175: I/dalvikvm-heap(30452): Grow heap (frag case) to 17.982MB for 5251824-byte allocation
03-20 11:20:36.260: I/dalvikvm-heap(30452): Grow heap (frag case) to 38.014MB for 21007248-byte allocation
03-20 11:20:36.385: I/dalvikvm-heap(30452): Grow heap (frag case) to 38.016MB for 5251824-byte allocation
03-20 11:20:36.480: I/dalvikvm-heap(30452): Grow heap (frag case) to 58.050MB for 21007248-byte allocation

【问题讨论】:

    标签: android out-of-memory


    【解决方案1】:

    Bitmap.recycle() 是您的朋友,因为它可以比手动或自动垃圾回收更快地释放图像的像素数据。因此,当您完成一个大图像时,最好在允许它超出范围之前recycle() 它可能是一个好主意。当然它超出范围后会自动被垃圾回收,但是recycle()会立即释放Bitmap使用的绝大部分内存。

    例如,您可以在将Bitmap 复制到 UI 元素后选择recycle()

    Bitmap bmp;
    bmp=decodeSampledBitmapFromResource(getResources(),R.drawable.panorama, reqwidth, reqheight);
    imgPanorama.setImageBitmap(bmp);
    
    // Free the pixel data immediately in order to keep heap from growing out of control.
    // This is especially useful while displaying multiple images.
    bmp.recycle();
    

    您还可以尝试使用较小的 reqwidthreqheight 值,这将允许较大的计算 inSampleSize,这样您的 Bitmap 将使用更少的内存。

    【讨论】:

    • 如你所说,很好地尝试回收位图。它给出了错误,例如不能使用已经回收的位图。
    • 图像 'bmp' 在 'bmp.recycle()' 调用后不能使用,除非是 'getWidth()' 和 'getHeight()' 等非像素访问函数。如果它在“setImageBitmap()”之后使用,然后移动“bmp.recycle()”调用。
    • 你是对的,我使用的是高度和宽度函数,请参见上面的代码。我在 Destroy() 中回收了它。
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 2012-06-17
    • 2012-10-25
    • 2014-10-24
    • 2012-12-07
    • 2013-01-26
    • 2014-05-13
    • 2015-11-22
    相关资源
    最近更新 更多