9

I am using android:background to give a background image to android layout.
After putting some images I get this exception :

08-24 00:40:19.237: E/dalvikvm-heap(8533): Out of memory on a 36000016-byte allocation.


how can I use large images as backgrounds on android?
can I expand the application heap memory? or is it something not good to do ?

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Evan Lévesque
  • 3,115
  • 7
  • 40
  • 61
  • 3
    As is mentioned in lots of these questions, don't use huge (> 30MB in your case) background images. – kabuko Aug 23 '13 at 21:49
  • 2
    There are lots of similar questions like this in the StackOverFlow. I have tried to make a summary of many alternatives to solve this: http://stackoverflow.com/questions/11820266/android-bitmapfactory-decodestream-out-of-memory-with-a-400kb-file-with-2mb-f/16528487#16528487 – Paulo Cheque Aug 23 '13 at 22:28
  • apart from not using a very big image, should consider the capacity of the devices, the real problem is that the device doesn't have the capacity to charge like background that huge image. – Maria Mercedes Wyss Alvarez Aug 23 '13 at 22:28
  • 30MB is not only one image, and thanks @PauloCheque – Evan Lévesque Aug 23 '13 at 22:36
  • @jahroy I said "after putting some images" so its not only one image. and no need to the down vote – Evan Lévesque Aug 23 '13 at 22:38

6 Answers6

9

Please have a look at my related question:

High resolution Image - OutOfMemoryError

Try to minimize the memory usage of your application by keeping the background image as small as possible.

This can be done via:

  • cropping the image so that it fits the screen
  • compress the image further (use e.g. photoshop) before even using it in the app
  • use the below method to load your bitmap
  • recycle the bitmap as soon as you no loger need it
  • make sure you do not keep multiple instances of it in memory
  • set the reference to null after using the bitmap

Make sure the images you set as a background are loaded properly (e.g. cropped in size, for example fit the screen size) and released from the memory as soon as they are no longer needed.

Make sure you have only one instance of your Bitmap in memory. After displaying it, call recycle() and set your reference to null.

This is how you could load your images:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

Thanks to Adam Stelmaszczyk for this beautiful piece of code.

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
7

I have experienced a similar issue due to background images in layouts. The normal size of a memory allocation of an image should be height*width*4 bytes (in mode ARGB_8888, the default mode).

If you see and allocation of 30MB when showing an activity there must be some problem. Check if you are placing your background images in the drawable folder. In that case, the system must scale that image to the specific density of the screen, causing a big memory overhead.

Solutions:

  1. Place a specific version of the background image in each drawable folder (mdpi, hdpi, xhdpi...).
  2. Place the background image in a special resource folder called "drawable-nodpi". The system won't try to scale images placed in this directory so only an streching process is carried out and the memory allocated will be the expected.

More information in this answer

Hope this helps.

Community
  • 1
  • 1
Martillador81
  • 328
  • 4
  • 6
6

I had the same problem and fixed it by the following:

  1. Create a drawable-nodpi folder and put your background images in there.

  2. Use Picasso to display the images http://square.github.io/picasso/

Display them with .fit() and .centerCrop() and Picasso scales the image if it needs to

ImageView ivLogo = (ImageView) findViewById(R.id.ivLogo);
Picasso.with(getApplicationContext())
   .load(R.drawable.logo)
   .fit()
   .centerCrop()
   .into(ivLogo);

If you do run out of memory somehow, Picasso will simply not display the image, instead of giving you an OOM error. I Hope this helps!

Nickmccomb
  • 1,467
  • 3
  • 19
  • 34
1

can I expand the application heap memory?

Yes, you can. Just set android:largeHeap:="true"in your Manifest.xml.

   <application
        android:name="com.pepperonas.libredrive.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:largeHeap="true"
        android:label="@string/app_name"
        android:launchMode="standard">
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
  • This doesn't work, apps can still run out of memory, and also slow your app down, with largeHeap on. https://developer.android.com/training/articles/memory.html Additionally, the large heap size is not the same on all devices and, when running on devices that have limited RAM, the large heap size may be exactly the same as the regular heap size. So even if you do request the large heap size, you should call getMemoryClass() to check the regular heap size and strive to always stay below that limit. – Nickmccomb Jan 04 '16 at 08:56
  • For me setting this feature solved an issue in an map fragment (which shows up to 30k geo marker)... And yes, it's clear, that every device has limited RAM - so we should produce code that consumes the lowest resources possible (sometimes its better to cache data, sometimes it's more recommendable to make use of the cpu). At this point nobody can give a short answer. But as I said, this snippet above did the trick for me in some circumstances.. so it's wrong to say "this doesn't work". – Martin Pfeffer Jan 04 '16 at 15:02
  • I don't agree. I believe a solution should not slow down your app and introduce more problems down the road. The app is not working because it is making bad use of bitmaps, causing an Out Of Memory error. Your "solution" would not fix this because, even if the bitmap is under the size of the available memory with your fix (which it may not be), once more bitmaps are introduced it definitely will be, and the OOM error will still occur. That's not a solution. – Nickmccomb Jan 05 '16 at 11:18
  • Just a minute ago I had a memory issue while loading a large image in an app-intro. On the Nexus 6p (real device) there was no problem at all, but genymotion was crashing because of the memory. after changing the param in the manifest the emulator runs stable as it should. Hehe, this is just what happened to me. Maybe you should give this trick a try next time :) – Martin Pfeffer Jan 06 '16 at 23:49
  • Here is what logcat says: Process: com.pepperonas.m104, PID: 21668 java.lang.OutOfMemoryError: Failed to allocate a 32778252 byte allocation with 15391484 free bytes and 14MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) – Martin Pfeffer Jan 06 '16 at 23:51
  • I can reproduce the error. See here: https://youtu.be/myerOO5Gr38 (please note that I am running AS 2.0, so the first attempt to relaunch the app, the app wasn't restarted completely) – Martin Pfeffer Jan 07 '16 at 00:01
0

Use Glide which will help you cache images and load the from memory

Glide  
    .with(/*context*/)
    .load(/*urlToImage*/)
    .thumbnail(0.5f/*scale*/)  //you can even downscale the image
    .into(imageView);

Secondly with Glide you can pause and resume request by observing scrolling behaviour by using the OnScrollChangedListener

if(scrollState == SCROLL_STATE_IDLE){
    Glide.with(this /*context*/).resumeRequests();
} else if(scrollState == SCROLL_STATE_TOUCH_SCROLL ||
          scrollState == SCROLL_STATE_FLING){
 Glide.with(this).pauseRequests();
}
Vincent Mungai
  • 202
  • 3
  • 4
0

For bitmap Glide is best .

Glide.with(getContext()).load(img_url_1).apply(new RequestOptions().override(Target.SIZE_ORIGINAL)).into(imageView1);