正如标题所说,我在加载布局时抛出了内存不足异常。有人会认为这是一个内存泄漏问题,但经过 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
这是其中一个视图的屏幕截图,以了解此处使用了多少图像