正如标题所说,我在加载布局时抛出了内存异常。有人会认为这是一个内存泄漏问题,但经过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图像的相关答案存储在本机内存中,但是this dude声称它们应该是自Android 3以来的堆,所以我应该看到内存增加,如果这确实是图像内存泄漏
我努力的最终结果是我仍然出现内存错误,尽管没有以前那么快。在发生错误之前,我开始看到可见的位图损坏,在我添加cleanupDrawables()
代码之前并非如此。我推断,对Bitmap.recycle()
的调用导致了损坏,即使此代码仅在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
以下是其中一个视图的屏幕截图,以了解此处使用了多少图像
答案 0 :(得分: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文件夹(如果不存在),并在其中放置位图的副本。