6

正如标题所说,我在加载布局时抛出了内存不足异常。有人会认为这是一个内存泄漏问题,但经过 2 天的斗争后,我不再那么确定了。该应用程序有 10 多个活动,其中大部分都有背景图像。

我所做的一些事实/发现:

  • 到目前为止,问题仅出现在运行 Android 4.0.3 的 Galaxy Nexus 上。我无法在 Nexus S (4.1.1) 和 Galaxy S II (2.3.3) 上重现它。

  • 屏幕方向不会改变。事实上,无论如何,我的大部分活动都锁定在肖像上。

  • 只是为了笑,我在打开新活动时添加了调用finish(),因此当时内存中不会有多个活动。已验证onDestroy正在调用。

定义为:

@Override
public void onDestroy()
{
    super.onDestroy();
    cleanupDrawables(contentView);

    // null all the fields referencing views and drawables

    System.gc();
}

哪里cleanupDrawables()是:

protected static void cleanupDrawables(View view)
{
    cleanupDrawable(view.getBackground());

    if (view instanceof ImageView)
        cleanupDrawable(((ImageView)view).getDrawable());
    else if (view instanceof TextView)
    {
        TextView tv = (TextView)view;
        Drawable[] compounds = tv.getCompoundDrawables();
        for (int i = 0; i < compounds.length; i++)
            cleanupDrawable(compounds[i]);
    }
    else if (view instanceof ViewGroup && !(view instanceof AdapterView))
    {
        ViewGroup vg = (ViewGroup)view;
        for (int i = 0; i < vg.getChildCount(); i++)
            cleanupDrawables(vg.getChildAt(i));
        vg.removeAllViews();
    }
}

protected static void cleanupDrawable(Drawable d)
{
    if (d == null)
        return;

    d.setCallback(null);

    if (d instanceof BitmapDrawable)
        ((BitmapDrawable)d).getBitmap().recycle();
    else if (d instanceof LayerDrawable)
    {
        LayerDrawable layers = (LayerDrawable)d;
        for (int i = 0; i < layers.getNumberOfLayers(); i++)
            cleanupDrawable(layers.getDrawable(i));
    }
}
  • 查看 Eclipse 堆检查器,内存似乎是稳定的,即某些活动比其他活动占用更多内存,但它在关闭时被释放,并且随着时间的推移它看起来很稳定。

  • 根据有关 SO 图像的相关答案,图像存储在本机内存中,但是这个家伙声称自 Android 3 以来它们应该在堆上,所以如果这确实是图像内存泄漏,我应该会看到内存增加。

我努力的最终结果是我仍然会出现内存不足错误,尽管没有以前那么快。在发生错误之前,我开始看到可见的位图损坏,而在我添加cleanupDrawables()代码之前情况并非如此。我推断调用Bitmap.recycle()会导致损坏,即使此代码仅被调用 on onDestroy。损坏出现在作为出现在许多活动上的常见样式的一部分的位图上以及仅在一个活动上显示的位图上。

总之,我的调查结果是相当不确定的。在这一点上,我不知道还能尝试什么。

错误堆栈跟踪供参考:

08-22 10:49:51.889: E/AndroidRuntime(31697): java.lang.RuntimeException: Unable to start activity ComponentInfo{klick.beatbleeds/klick.beatbleeds.Bleeds}: android.view.InflateException: Binary XML file line #67: Error inflating class <unknown>
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.os.Looper.loop(Looper.java:137)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread.main(ActivityThread.java:4340)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at dalvik.system.NativeStart.main(Native Method)
08-22 10:49:51.889: E/AndroidRuntime(31697): Caused by: android.view.InflateException: Binary XML file line #67: Error inflating class <unknown>
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at klick.beatbleeds.ActivityBase.setContentView(ActivityBase.java:82)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at klick.beatbleeds.Bleeds.onCreate(Bleeds.java:40)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.Activity.performCreate(Activity.java:4465)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-22 10:49:51.889: E/AndroidRuntime(31697):    ... 11 more
08-22 10:49:51.889: E/AndroidRuntime(31697): Caused by: java.lang.reflect.InvocationTargetException
08-22 10:49:51.889: E/AndroidRuntime(31697):    at java.lang.reflect.Constructor.constructNative(Native Method)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
08-22 10:49:51.889: E/AndroidRuntime(31697):    ... 23 more
08-22 10:49:51.889: E/AndroidRuntime(31697): Caused by: java.lang.OutOfMemoryError
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.Bitmap.nativeCreate(Native Method)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.content.res.Resources.loadDrawable(Resources.java:1937)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.View.<init>(View.java:2780)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.view.ViewGroup.<init>(ViewGroup.java:385)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.widget.LinearLayout.<init>(LinearLayout.java:174)
08-22 10:49:51.889: E/AndroidRuntime(31697):    at android.widget.LinearLayout.<init>(LinearLayout.java:170)
08-22 10:49:51.889: E/AndroidRuntime(31697):    ... 26 more

这是其中一个视图的屏幕截图,以了解此处使用了多少图像 在此处输入图像描述

4

1 回答 1

2

使用这些静态方法找出确切的问题:

public static void showBitmapSize(Bitmap bitmap) {
    Log.d("test", "bitmap dimensions: w:" + bitmap.getWidth() + ", h:" + bitmap.getHeight() + " memory: " + (bitmap.getRowBytes() * bitmap.getHeight() / 1048576d));
}

还有最重要的:

static double lastavail;
static double initavail;
static boolean first = true;

public static void showMemoryStats() {
    showMemoryStats("");
}

public static void showMemoryStats(String message) {
    Log.i("memory", message + "----------------------------------------------------------------------------------------");
    double nativeUsage = Debug.getNativeHeapAllocatedSize(); 
    Log.i("memory", "nativeUsage: " + (nativeUsage / 1048576d));
    //current heap size 
    double heapSize =  Runtime.getRuntime().totalMemory();
//      Log.i("memory", "heapSize: " + (heapSize / 1048576d));
    //amount available in heap 
    double heapRemaining = Runtime.getRuntime().freeMemory();
//      Log.i("memory", "heapRemaining: " + (heapRemaining / 1048576d)); 
    double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
    Log.i("memory", "memoryAvailable: " + (memoryAvailable / 1048576d));

    if (first) {
        initavail = memoryAvailable;
        first = false;
    }
    if (lastavail > 0) {
        Log.i("memory", "consumed since last: " + ((lastavail - memoryAvailable) / 1048576d));
    }
    Log.i("memory", "consumed total: " + ((initavail - memoryAvailable) / 1048576d));

    lastavail = memoryAvailable;

    Log.i("memory", "-----------------------------------------------------------------------------------------------");
}

除以 1048576 只是为了得到以 MB 为单位的值(至少对我来说,以 MB 为单位更容易思考)。

在调用showMemoryStats之前用一些有意义的信息setContentView()打一个电话,在它之后再打一个。当你开始一项新的活动时,等等。最后你会知道你的问题的确切原因。

手动回收可能是必要的。我必须在我的应用程序的某些地方实现它。还使用位图密集型(许多背景和图片)并且在所有设备中都存在这种问题。通过这些方法,我能够找到所有问题并妥善处理。

啊。你的问题有一个可能的快速解决方案,你说它只出现在 Galaxy Nexus 中。这是您提到的唯一 xhdpi 设备。您可能仅在文件夹 drawable 或 drawable-hdpi 中拥有所有位图。xhdpi 设备将从 drawable 或 drawable-hdpi 获取位图并放大(尽管它们可能已经具有正确的大小),这将消耗大量内存。解决方案:创建 drawable-xhdpi 文件夹,如果不存在,并将位图的副本放在那里。

于 2012-08-22T17:27:11.673 回答