2

我有一个包含 12 个 ImageViews 的活动。在 onCreate 方法中,我使用BitmapFactory.decodeStream(...)在这些 ImageViews 中加载 12 张照片。

当我来到这个页面并返回 10 次时,我得到以下内存不足异常。

为什么会出现此错误,如何释放资源以防止出现此异常?

谢谢,

04-24 14:41:00.050: D/TextView(7586): Constructor - Got appearance for textColorPrimaryInverse
04-24 14:41:00.050: D/TextView(7586): Constructor -- Got mEditTextBackgroundColor
04-24 14:41:00.130: I/dalvikvm-heap(7586): Clamp target GC heap from 48.878MB to 48.000MB
04-24 14:41:00.130: D/dalvikvm(7586): GC_FOR_ALLOC freed 495K, 2% free 47950K/48775K, paused 44ms
04-24 14:41:00.130: I/dalvikvm-heap(7586): Forcing collection of SoftReferences for 404616-byte allocation
04-24 14:41:00.180: I/dalvikvm-heap(7586): Clamp target GC heap from 48.856MB to 48.000MB
04-24 14:41:00.180: D/dalvikvm(7586): GC_BEFORE_OOM freed 23K, 2% free 47926K/48775K, paused 48ms
04-24 14:41:00.180: E/dalvikvm-heap(7586): Out of memory on a 404616-byte allocation.
04-24 14:41:00.180: I/dalvikvm(7586): "main" prio=5 tid=1 RUNNABLE
04-24 14:41:00.180: I/dalvikvm(7586):   | group="main" sCount=0 dsCount=0 obj=0x40a54460 self=0x1ad5828
04-24 14:41:00.180: I/dalvikvm(7586):   | sysTid=7586 nice=0 sched=0/0 cgrp=default handle=1074742408
04-24 14:41:00.180: I/dalvikvm(7586):   | schedstat=( 14073874000 2242143000 19299 ) utm=1301 stm=106 core=1
04-24 14:41:00.180: I/dalvikvm(7586):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
04-24 14:41:00.180: I/dalvikvm(7586):   at org.mabna.order.utils.Utilities.decodeFile(Utilities.java:1220)
04-24 14:41:00.180: I/dalvikvm(7586):   at org.mabna.order.businessLayer.db.BoInvProducts.getProductPhoto(BoInvProducts.java:107)
04-24 14:41:00.180: I/dalvikvm(7586):   at org.mabna.order.ui.ActProductsGallery.loadPageImages(ActProductsGallery.java:729)
04-24 14:41:00.180: I/dalvikvm(7586):   at org.mabna.order.ui.ActProductsGallery.selectItemInGallery(ActProductsGallery.java:611)
04-24 14:41:00.180: I/dalvikvm(7586):   at org.mabna.order.ui.ActProductsGallery.onCreate(ActProductsGallery.java:315)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.Activity.performCreate(Activity.java:4465)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.os.Looper.loop(Looper.java:137)
04-24 14:41:00.180: I/dalvikvm(7586):   at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 14:41:00.180: I/dalvikvm(7586):   at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:41:00.180: I/dalvikvm(7586):   at java.lang.reflect.Method.invoke(Method.java:511)
04-24 14:41:00.180: I/dalvikvm(7586):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 14:41:00.180: I/dalvikvm(7586):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 14:41:00.180: I/dalvikvm(7586):   at dalvik.system.NativeStart.main(Native Method)
04-24 14:41:00.180: D/skia(7586): --- decoder->decode returned false
04-24 14:41:00.180: D/AndroidRuntime(7586): Shutting down VM
04-24 14:41:00.180: W/dalvikvm(7586): threadid=1: thread exiting with uncaught exception (group=0x40a531f8)
04-24 14:41:00.180: E/AndroidRuntime(7586): FATAL EXCEPTION: main
04-24 14:41:00.180: E/AndroidRuntime(7586): java.lang.OutOfMemoryError
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at org.mabna.order.utils.Utilities.decodeFile(Utilities.java:1220)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at org.mabna.order.businessLayer.db.BoInvProducts.getProductPhoto(BoInvProducts.java:107)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at org.mabna.order.ui.ActProductsGallery.loadPageImages(ActProductsGallery.java:729)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at org.mabna.order.ui.ActProductsGallery.selectItemInGallery(ActProductsGallery.java:611)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at org.mabna.order.ui.ActProductsGallery.onCreate(ActProductsGallery.java:315)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.Activity.performCreate(Activity.java:4465)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.os.Looper.loop(Looper.java:137)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 14:41:00.180: E/AndroidRuntime(7586):     at dalvik.system.NativeStart.main(Native Method)
04-24 14:41:01.350: I/Process(7586): Sending signal. PID: 7586 SIG: 9
4

3 回答 3

2

也许,用您的最接近的尺寸解码您的图像ImageViews(请参阅:将图像加载到位图对象时出现奇怪的内存不足问题

于 2012-04-23T10:26:33.667 回答
1

听起来您以错误的方式切换活动并在内存中保留了许多实例。您可以尝试在打开此活动的意图上设置 FLAG_ACTIVITY_CLEAR_TOP 或查看活动生命周期以确定应在何处加载图像。

于 2012-04-23T10:23:45.367 回答
0

您是否尝试过以下操作?

  1. 像这样回收位图

    @Override
    public void onDestroy() {
    super.onDestroy();
    for (int i = 0; i < 5; i++) {
        TermElement e = theFiveGaugesInclRPMInPos4[i];
        e.background.recycle();
        e.background = null;
    }
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " <<-"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    }
    
  2. 为了重用位图,我将其定义为静态,并像这样初始化:

      if (faceTexture == null)
        faceTexture = BitmapFactory.decodeResource(getContext()
            .getResources(), faceTextureID);
    
  3. 使用 DDMS 检查内存使用情况

现在它不再崩溃了。

ps 对格式化感到抱歉 - 它只是不再让我正确格式化代码 - 必须重新启动?:-(

于 2012-04-23T11:12:30.180 回答