Rohit
Rohit

Reputation: 7161

Loading resources onCreate casusing java.lang.OutOfMemoryError

I am adding some resources onCreate, it works fine on Android 2.2 to 2.3.x but crashes on 4.0+ below is the code snippet

I have placed all the resources in drawable-mdpi, can that be issue?

private List<Drawable> drawables;
private void getDrawablesList() {

    drawables = new ArrayList<Drawable>();
    for(int i=0; i<26; i++)
        drawables.add(getResources().getDrawable(Utility.getImageIDofGalleryAlphabets(i)));

}
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    getDrawablesList();
    setupUI();
}

i get exception in getDrawablesList following line

drawables.add(getResources().getDrawable(Utility.getImageIDofGalleryAlphabets(i)));

Below is the crash log:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
    at android.content.res.Resources.loadDrawable(Resources.java:1937)
    at android.content.res.Resources.getDrawable(Resources.java:664)
    at com.rdx.gallery.GalleryDemoActivity.getDrawablesList(GalleryDemoActivity.java:180)
    at com.rdx.gallery.GalleryDemoActivity.onCreate(GalleryDemoActivity.java:65)
    at android.app.Activity.performCreate(Activity.java:4635)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2031)
    at android.app.ActivityThread.access$600(ActivityThread.java:126)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1166)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4486)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
04-01 08:31:28.894: I/Process(11767): Sending signal. PID: 11767 SIG: 9
04-01 08:31:28.894: E/AndroidRuntime(11767): Handle UnCaght exceptions. KILLING PID: 11767

I know I am loading 26 resources in beginning. The total size of 26 images = 2.57 MB, which I donot think is too large..

What is going wrong? Please share your views

Upvotes: 0

Views: 904

Answers (1)

Martin
Martin

Reputation: 4846

While it may be only 2.5M to you, Android will scale your resources depending on the pixel density and display size of your device. It may turn out to be more, once loaded and ready for use. Additionally, some devices just don't have lots of memory. You might have only 16M on some devices, and you're sharing that space with Android as well as any other app that may be running in the background. My guess is that your 4.0 device is using more memory for Android than the 2.x device and you're running out of memory earlier.

If you really need all of these images, perhaps you can architect a solution that loads only what is needed at the time, and releases those it doesn't need. Pain in the butt, but memory is pretty limited on these devices

Upvotes: 1

Related Questions