Android的内存溢出错误位大小超过2.3.3 VM预算 [英] Android outofmemory error bitmap size exceeds vm budget in 2.3.3

查看:141
本文介绍了Android的内存溢出错误位大小超过2.3.3 VM预算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这个问题是问几次。他们没有一个清晰的解决方案。让我来解释这个问题。

  1. 在我有一个活动,它加载4图像的时间。
  2. 我在onResume()方法加载图像。
  3. 在该活动加载时会引发位错误。

注。

  1. 在我使用setImageResource(R.drawable.xxxx)方法调用,而不是位图/直接绘区域设置图像。
  2. 的图像被正确地缩放。
  3. 的activtiy工作正常,在2.3之前,所有的仿真器和工作正常在实际设备(三星Galaxy 5)
  4. 的错误出现在第一个初始化,没有方向变化事件。
  5. 图像是800×600的规模和平均15KB(每个)的大小。

让我知道任何解决方案。也让我知道,如果你有采用Android 2.3.3模拟器类似的问题。

[更新] -snippets

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
            ...
    img_topLeft =(ImageView的)findViewById(R.id.Img_Alph_Q_TopLeft);
    img_topRight =(ImageView的)findViewById(R.id.Img_Alph_Q_TopRight);
    img_bottomLeft =(ImageView的)findViewById(R.id.Img_Alph_Q_BottomLeft);
    img_bottomRight =(ImageView的)findViewById(R.id.Img_Alph_Q_BottomRight);
   ...
   }
保护无效onResume(){
    super.onResume();
            img_topLeft.setImageResource(R.drawable.xxx);
            img_topRight.setImageResource(R.drawable.xxx);
            img_bottomLeft.setImageResource(R.drawable.xxx);
            img_bottomRight.setImageResource(R.drawable.xxx);
   ...
   }
 

  

8月3号至21日:59:17.362:ERROR / dalvikvm堆(5883):432万字节的外部分配太大,此过程。   八月3号至21号:59:17.412:ERROR / GraphicsJNI(5883):虚拟机不会让我们分配4320000字节   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):致命异常:主要   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):java.lang.OutOfMemoryError:位图大小超过VM预算   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.Bitmap.nativeCreate(本机方法)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.Bitmap.createBitmap(Bitmap.java:477)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.Bitmap.createBitmap(Bitmap.java:444)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.BitmapFactory.finishDe code(BitmapFactory.java:498)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:473)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:336)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.content.res.Resources.loadDrawable(Resources.java:1709)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.content.res.Resources.getDrawable(Resources.java:581)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.widget.ImageView.resolveUri(ImageView.java:501)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.widget.ImageView.setImageResource(ImageView.java:280)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在Quiz.java:124)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):Quiz.onResume(Quiz.java:92)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.Activity.performResume(Activity.java:3832)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.os.Handler.dispatchMessage(Handler.java:99)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.os.Looper.loop(Looper.java:123)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在android.app.ActivityThread.main(ActivityThread.java:3683)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在java.lang.reflect.Method.invokeNative(本机方法)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在java.lang.reflect.Method.invoke(Method.java:507)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)   八月3号至21号:59:17.432:ERROR / AndroidRuntime(5883):在dalvik.system.NativeStart.main(本机方法)

感谢。设法解决它。共享code为他人的利益 这解决了这个问题,自定义类。根据@ Janardhanan.S链接。

 公共类Bitma presizer {

公共静态位图德$ C $的CImage(资源RES,INT ID,INT requiredSize){
    尝试 {
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeResource(RES,ID,O);

        //找到正确的比例值。它应该是2的幂。
        最终诠释REQUIRED_SIZE = requiredSize;
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }

        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeResource(RES,ID,O2);
    }赶上(例外五){

    }
    返回null;
}

}

//类调用
INT requiredsize = 100; //仍然在摆弄这个号码,找到最佳值
img_topLeft.setImageBitmap(Bitma presizer.de $ C $的CImage(getResources()
        AlphResourceSet.R.drawable.xxx,requiredsize));
 

解决方案

位图会消耗大量的内存空间。 不给你上的活动加载所有的图像创建一个新的位图的变量, 相反,你可以创建一个位图变量和重用他们尽可能多的就可以了。

您可以使用此代码段来调整位图

http://pastebin.com/D8vbQd2u

I understand that this question is asked few times. None of them is clear on the solution. Let me explain the problem.

  1. I have an Activity which loads 4 images at a time.
  2. I load the images in the onResume() method.
  3. The Activity throws bitmap error when loading.

Notes.

  1. I am setting image using setImageResource(R.drawable.xxxx) method call and not bitmap/drawables directly.
  2. The images are scaled correctly.
  3. The activtiy WORKS FINE in all emulators prior to 2.3 and is WORKING FINE in Actual device (Samsung Galaxy 5)
  4. The error is appearing on first initialisation and no orientation change event is fired.
  5. images are 800 x 600 in scale and averages 15kb(each) in size.

Let me know any solutions. Also let me know if you have similar issues with Android 2.3.3 emulator.

[update] -snippets

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            ...
    img_topLeft = (ImageView) findViewById(R.id.Img_Alph_Q_TopLeft);
    img_topRight = (ImageView) findViewById(R.id.Img_Alph_Q_TopRight);
    img_bottomLeft = (ImageView) findViewById(R.id.Img_Alph_Q_BottomLeft);
    img_bottomRight = (ImageView) findViewById(R.id.Img_Alph_Q_BottomRight);
   ...
   }
protected void onResume() {
    super.onResume();
            img_topLeft.setImageResource(R.drawable.xxx);
            img_topRight.setImageResource(R.drawable.xxx);
            img_bottomLeft.setImageResource(R.drawable.xxx);
            img_bottomRight.setImageResource(R.drawable.xxx);
   ...
   }

03-21 08:59:17.362: ERROR/dalvikvm-heap(5883): 4320000-byte external allocation too large for this process. 03-21 08:59:17.412: ERROR/GraphicsJNI(5883): VM won't let us allocate 4320000 bytes 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): FATAL EXCEPTION: main 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.nativeCreate(Native Method) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createBitmap(Bitmap.java:477) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createBitmap(Bitmap.java:444) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.content.res.Resources.loadDrawable(Resources.java:1709) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.content.res.Resources.getDrawable(Resources.java:581) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.widget.ImageView.resolveUri(ImageView.java:501) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.widget.ImageView.setImageResource(ImageView.java:280) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at Quiz.java:124) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): Quiz.onResume(Quiz.java:92) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.Activity.performResume(Activity.java:3832) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.os.Handler.dispatchMessage(Handler.java:99) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.os.Looper.loop(Looper.java:123) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at java.lang.reflect.Method.invokeNative(Native Method) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at java.lang.reflect.Method.invoke(Method.java:507) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at dalvik.system.NativeStart.main(Native Method)

Thanks. Managed to resolve it. Sharing the code for benefit of others Custom class that resolved this issue. based on @Janardhanan.S link.

public class BitmapResizer {

public static Bitmap decodeImage(Resources res, int id ,int requiredSize){
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, id, o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=requiredSize;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeResource(res, id, o2);
    } catch (Exception e) {

    }
    return null;
}

}

//Class call
int requiredsize = 100; // Still playing around with this number to find the optimum value
img_topLeft.setImageBitmap(BitmapResizer.decodeImage(getResources(),
        AlphResourceSet.R.drawable.xxx, requiredsize));

解决方案

Bitmap consumes a lot of memory space. dont create a new bitmap variable for all image you load on activity, instead you can create one bitmap variable and reuse them as much as you can.

you can use this snippet to resize bitmap

http://pastebin.com/D8vbQd2u

这篇关于Android的内存溢出错误位大小超过2.3.3 VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆