我写了一个方法来调整drawable的大小,但我发现它泄漏了。它在我的游戏的SinglePlayer活动中实现,所以当我重新启动该活动(Play Again按钮)时,可用的堆大小会减少,直到它最终崩溃。我想关闭一项活动并且没有遗留任何痕迹。我确定ResizeDrawable方法是唯一导致崩溃的方法,因为当我删除它时,问题就解决了。这是代码:
//They don't have to be global, I guess it makes no difference
Bitmap temp;
Bitmap resbit;
Drawable resized;
Drawable ResizeDrawable(Drawable image,int width,int height)
{
temp = ((BitmapDrawable)image).getBitmap();
resbit = Bitmap.createScaledBitmap(temp, width, height, true);
resized = new BitmapDrawable(getResources(), resbit );
return resized;
}
那么为什么这会填满内存?如何在活动结束后修改此方法或整个活动以释放所有内存?
更新
我尝试使用recycle()方法。据我所知,回收可以释放位图的内存,因此可以在没有泄漏的情况下再次使用它。我添加了这些内容:
if (temp != null)
{
temp.recycle();
temp = null;
}
if (resbit != null)
{
resbit .recycle();
resbit = null;
}
1)在ResizeDrawable方法的初始化 2)返回陈述之前
两次编辑都导致了#34;尝试使用回收的位图"崩溃。我哪里出错了?
更新
我尝试删除" resbit recycle"部分。现在我能够完全重启我的活动2次,然后崩溃了相同的异常。怎么可能?
更新
我尝试用:
覆盖OnDestroy()Runtime.getRuntime().gc();
和
finish();
没有任何改变。还尝试用回收覆盖OnDestroy()。相同"恰好2次"崩溃。
日志:
因此原始代码在SinglePlayer活动完全重新启动16次后崩溃了。日志:
11-16 03:46:31.115: E/AndroidRuntime(9649): FATAL EXCEPTION: main
11-16 03:46:31.115: E/AndroidRuntime(9649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.starena/com.example.starena.SinglePlayer}: android.view.InflateException: Binary XML file line #40: Error inflating class <unknown>
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.os.Looper.loop(Looper.java:130)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread.main(ActivityThread.java:3701)
11-16 03:46:31.115: E/AndroidRuntime(9649): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 03:46:31.115: E/AndroidRuntime(9649): at java.lang.reflect.Method.invoke(Method.java:507)
11-16 03:46:31.115: E/AndroidRuntime(9649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
11-16 03:46:31.115: E/AndroidRuntime(9649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
11-16 03:46:31.115: E/AndroidRuntime(9649): at dalvik.system.NativeStart.main(Native Method)
11-16 03:46:31.115: E/AndroidRuntime(9649): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class <unknown>
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
11-16 03:46:31.115: E/AndroidRuntime(9649): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-16 03:46:31.115: E/AndroidRuntime(9649): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.Activity.setContentView(Activity.java:1657)
11-16 03:46:31.115: E/AndroidRuntime(9649): at com.example.starena.SinglePlayer.onCreate(SinglePlayer.java:177)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-16 03:46:31.115: E/AndroidRuntime(9649): ... 11 more
11-16 03:46:31.115: E/AndroidRuntime(9649): Caused by: java.lang.reflect.InvocationTargetException
11-16 03:46:31.115: E/AndroidRuntime(9649): at java.lang.reflect.Constructor.constructNative(Native Method)
11-16 03:46:31.115: E/AndroidRuntime(9649): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
11-16 03:46:31.115: E/AndroidRuntime(9649): ... 22 more
11-16 03:46:31.115: E/AndroidRuntime(9649): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:494)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.content.res.Resources.loadDrawable(Resources.java:1720)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.widget.ImageView.<init>(ImageView.java:122)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.widget.ImageButton.<init>(ImageButton.java:85)
11-16 03:46:31.115: E/AndroidRuntime(9649): at android.widget.ImageButton.<init>(ImageButton.java:81)
11-16 03:46:31.115: E/AndroidRuntime(9649): ... 25 more
我是如何使用它的:
所以在我的OnCreate()方法中,我初始化了几个ImageButtons和他们将使用的几个Drawable。它看起来像这样:
Drawable1 = context.getResources().getDrawable(R.drawable.drawable1);
Drawable1 = ResizeDrawable(Drawable1,ButtonWidth,ButtonHeight);
ImgButton1 = (ImageButton) findViewById(R.id.ImageButton02);
ImgButton1.setOnClickListener(this);
ImgButton1.setImageDrawable(Drawable1);
此外,每个图像按钮都有两个drawable,我通过OnClick()方法在它们之间交替(我只是setImageDrawable()一个或另一个)。
我希望这会有所帮助。
解决: 我不得不用以下内容覆盖OnDestroy:
Drawable1 = null;
ImgButton1.setImageDrawable(null);
当然我为每个(ImgButton,Drawable)对做了它,它停止消耗内存。我没有回收位图。
答案 0 :(得分:2)
尝试为未使用的Bitmap实例调用Bitmap.recycle()。
<强> Bitmap.recycle()强>
http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()