geecko
geecko

Reputation: 667

OutOfMemoryError when processing Bitmaps for the second time

I'm processing many Bitmaps in an activity to create an AnimationDrawable and it works perfectly (even though it uses a lot of memory). But when the user presses back and returns to the MainActivity, the memory isn't freed up.

At that moment :

How do I free up that memory ? I tried setting all my variables to null at the end of the second activity, I tried calling system.gc() at various moments.

This part of the code should be sufficient :

try {
        d = (BitmapDrawable) BitmapDrawable.createFromStream(new BufferedInputStream(new FileInputStream(src)), null);
        b = ((BitmapDrawable) d).getBitmap();
        b = Bitmap.createScaledBitmap(b, 300, 450, true);
        d = new BitmapDrawable(b);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
        animation.addFrame(d,1000/fps); // animation is an AnimationDrawable

This part of the code is in a for loop, it's usually being repeated about between 70 and 100 times, animation is thus quite "heavy". How do I get rid of it ? I want the app to "forget it" as soon as the second activity is paused.

Thank you very much. :)

Upvotes: 2

Views: 280

Answers (1)

rciovati
rciovati

Reputation: 28063

Call .recycle() method when you don't need the Bitmap object anymore:

http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()

Upvotes: 1

Related Questions