I wrote a method in order to resize drawables but I found it leaks. It's implemented in SinglePlayer activity of my game, so when I restart that activity (the Play Again button) the available heap size is getting reduced until it finally crashes. I want to close an activity and have no traces left. I'm certain the ResizeDrawable method is the only thing causing crashes because when I remove it, the problem is solved. Here is the code:
//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;
}
So why does this fill the memory up and how can I modify this method or an entire activity in order to free all the memory once the activity is closed?
UPDATE:
I tried using recycle() method. As far as I understood recycling frees the memory of the bitmap so it can be used again with no leaks. I added these lines:
if (temp != null)
{
temp.recycle();
temp = null;
}
if (resbit != null)
{
resbit .recycle();
resbit = null;
}
1) At the beggining of the ResizeDrawable method 2) Before return statement
Both edits resulted in a "trying to use a recycled bitmap" crash. Where did I go wrong?
UPDATE:
I tried removing the "resbit recycle" part. Now I was able to restart my activity exactly 2 times and then it crashed with the same exception. How is that possible?
UPDATE:
I tried overriding OnDestroy() with:
Runtime.getRuntime().gc();
and:
finish();
Nothing changed. Also tried overriding OnDestroy() with the recycles. Same "exactly 2 times" crash.
LOGS:
So the original code crashes after exactly 16 restarts of the SinglePlayer activity. The log:
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
How I'm using it:
So in my OnCreate() method I have initialized several ImageButtons and several Drawables they will use. It looks like this:
Drawable1 = context.getResources().getDrawable(R.drawable.drawable1);
Drawable1 = ResizeDrawable(Drawable1,ButtonWidth,ButtonHeight);
ImgButton1 = (ImageButton) findViewById(R.id.ImageButton02);
ImgButton1.setOnClickListener(this);
ImgButton1.setImageDrawable(Drawable1);
Also, every Image Button has two drawables and I alternate between them via the OnClick() method( I just setImageDrawable() one or another).
I hope this helps.
SOLVED: I had to overide OnDestroy with:
Drawable1 = null;
ImgButton1.setImageDrawable(null);
Of course I did it for every (ImgButton,Drawable) pair and it stopped consuming memory. I didn't recycle bitmaps.