0

I have an app that shows a lot of activities, each one showing a bitmap that fits the 100% of the width of the screen and normally more than the 100% of the height.

When you touch the screen, a new activity is created, and in the previous activity is being called finish and recycle for the bitmap:

public void clean() {
    if(this.myBitmap != null){
        Log.d(DEBUG_TAG, " Cleaning  "+this);
        this.myBitmap.recycle();
        this.myBitmap=null;
        System.gc();
    }
}

the problem is that the memory required is growing and growing and when i am launching more than 12-13 activities these messages are being showed at logcat:

05-05 16:03:02.909: I/dalvikvm-heap(24794): Clamp target GC heap from 65.790MB to 64.000MB
05-05 16:03:02.969: D/dalvikvm(24794): GC_EXPLICIT freed 71K, 5% free 59008K/61680K, paused 2ms+13ms, total 55ms
05-05 16:03:03.559: I/dalvikvm-heap(24794): Clamp target GC heap from 67.197MB to 64.000MB
05-05 16:03:03.569: D/dalvikvm(24794): GC_EXPLICIT freed 418K, 2% free 60521K/61680K, paused 3ms+28ms, total 109ms

after a few activities more i got this crash:

05-05 16:03:05.049: E/AndroidRuntime(24794): java.lang.OutOfMemoryError
05-05 16:03:05.049: E/AndroidRuntime(24794):    at android.graphics.Bitmap.nativeCreate(Native Method)
05-05 16:03:05.049: E/AndroidRuntime(24794):    at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
05-05 16:03:05.049: E/AndroidRuntime(24794):    at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
05-05 16:03:05.049: E/AndroidRuntime(24794):    at android.graphics.Bitmap.createBitmap(Bitmap.java:718)

I dont know what is going wrong here, i am doing recycle() of the bitmap and also i tryed with and without System.gc(), in both cases i got the crash

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Do you add largeHeapSize flag to your app? – Sergey Shustikov May 05 '14 at 14:18
  • mmmm thanks deathember it seems that using that has been improved my app, but i think it is not the final solution – NullPointerException May 05 '14 at 14:35
  • Let me explain you. When you use big bitmaps in your memory you will give an OME. More bitmaps - more chance to get an OUME. Adding flag not a bad way because Android get you max heap size to load your objects into memory. Google it for Memory Managment for android of Roman Guy. Google recommended you to use this flag. But you have another way. Look at your code and may be you can improve it and maybe this fix your OME. – Sergey Shustikov May 05 '14 at 14:44

2 Answers2

2

Its a known bug, its not because of large files. Since Android Caches the Drawables, its going out of memory after using few images. But i found alternate way for it, by skipping the android default cache system.

Soultion: Create a drawable folder in Assets and move the images to "drawable" folder in assets and use the following function to get BitmapDrawable

public static Drawable getAssetImage(Context context, String filename) throws IOException {
    AssetManager assets = context.getResources().getAssets();
    InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
    Bitmap bitmap = BitmapFactory.decodeStream(buffer);
    return new BitmapDrawable(context.getResources(), bitmap);
}

Refrence : https://stackoverflow.com/posts/6116316/revisions

Also that add the line below in your manifest file

android:largeHeap="true"
Community
  • 1
  • 1
Quamber Ali
  • 2,170
  • 25
  • 46
1

Use DDMS to grab a heap dump and do some analysis on the dump using Eclipse Memory Analyzer (built into Eclipse ADT or can run stand-alone). What your looking for is objects that you allocated but are not being freed. One possibility is that you are leaking activities. You say the OOM error occurs after launching 12-13 activities. Only one of the activities should be visible at a time so you can free memory from the paused activities in the onPause() or onStop() method. Again, analyzing the heap dump will give you a good idea of what is eating up the heap memory.

FYI - Bitmap#recycle() is only really necessary on Gingerbread and lower devices. On newer version of the OS bitmaps are stored on the heap and are garbage collected like other objects. See, the "Manage Memory on Android 2.3.3 and Lower" section of this document: https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Matt Accola
  • 4,090
  • 4
  • 28
  • 37