为什么我得到OutOfMemoryException? (见下面的简单代码)

时间:2013-08-26 16:39:49

标签: android exception imageview android-memory

在我的示例应用程序中,我在布局中有一个ImageView:

<FrameLayout 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">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bigimage" />

</FrameLayout>

图像的尺寸为(宽*高):2288 * 1712 位图配置是默认值:ARGB_8888,因此内存中加载的图像大小为:2288 * 1712 * 4byte = 15 668 224 byte 我的设备上的堆的内存限制是128 MB(我所知道的Runtime.getRuntime()。maxMemory())。

如果我运行程序,我会得到OutofMemoryException。但为什么?我们来看下面的日志。 15668240字节是好的,这就是我上面计算的。到目前为止没问题。但是下面的141014032字节分配是什么?它来自哪里?

08-26 18:21:35.133: I/dalvikvm-heap(31986): Grow heap (frag case) to 34.295MB for 15668240-byte allocation
08-26 18:21:35.204: I/dalvikvm-heap(31986): Forcing collection of SoftReferences for 141014032-byte allocation
08-26 18:21:35.224: E/dalvikvm-heap(31986): Out of memory on a 141014032-byte allocation.

主要活动(唯一的活动)是:

package com.example.homokozo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

0 个答案:

没有答案