6

I'm getting java.lang.OutOfMemoryError when using bitmap to show large images.
For resizing the bitmap, I referred following link Save Bitmap and BitmapFactory.decodeFile.

My logcat is as follows:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError
    at     android.graphics.Bitmap.nativeCreate(Native Method)
    at     android.graphics.Bitmap.createBitmap(Bitmap.java:604)
    at     android.graphics.Bitmap.createBitmap(Bitmap.java:551)
    at     com.android.restaurant.DescriptionPage.getResizedBitmap(DescriptionPage.java:327)
    at         com.android.restaurant.DescriptionPage.setDetailsIntoLayout(DescriptionPage.java:172)
    at com.android.restaurant.DescriptionPage.onCreate(DescriptionPage.java:101)
    at android.app.Activity.performCreate(Activity.java:4397)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
    at android.app.ActivityThread.access$500(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4135)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60
  • 1
    There's either something wrong with your code, or the phone doesn't have enough memory. Either way, we can't help you, at least until you post your code. –  Apr 16 '12 at 06:11
  • @Raman-Mystry - please refer to the "Displaying Bitmaps Efficiently" article below. If you're still having problems, please 1) post the relevant code, 2) tell us something about your bitmap (like how big it is in h/w and filesize!), 3) tell us how much memory your system has before attempting to load the bitmap – paulsm4 Apr 16 '12 at 06:17
  • 1
    [Try it][1] [1]: http://stackoverflow.com/questions/10081008/outofmemoryerror-in-game-with-many-small-images/10086141#comment12999643_10086141 It will really help you. – Suvam Roy Apr 16 '12 at 06:21

2 Answers2

5

Since you have not posted your code it is hard to know whether there is a bug or probably ineffective code that can be improved.

But generally the JVM memory is limited, so loading of big image can cause OutOfMemoryError. You can try to increase the JVM memory using -Xmx option. This is the first and the simplest way.

Other way is to decrease your image (if it is possible). Or probably use format other than BMP (e.g. JPG). It guarantees good quality and requires less memory.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

The challenge in developing mobile application is that you have limited memory.

First thing stuck to my mind is try to use PNG(Portable Network graphics) instead of the BitMaps (if possible).

Apart from that, It seems that this is existing issue please check this:- http://code.google.com/p/android/issues/detail?id=8488

neo
  • 1,054
  • 1
  • 10
  • 19