Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of Memory Error while loading bitmaps

i have an android app with 3 acitivtys:

A1 --starts--> A2 --starts--> A3 --when finished his process: starts--> A1

(so i don't "finish();" an app. i start the next activitys with "startActivity(..);" the whole time after userinteraction)

so there is a loop in these 3 activitys. On each Activity, i display 3-9 pictures, located on the SD-card, which i load with my following function:

try
{
    Uri selectedImageURI = Uri.parse(strImagePath);
    File imgFile = new  File(getRealPathFromURI(selectedImageURI, c));
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ivTmp.setImageBitmap(myBitmap);
}catch (Exception e)
{
    return null;
}

This all works. But sometimes (after looping a few times through my activitys), my app crashes..

Logcat tells me:

01-16 13:42:15.863: DEBUG/dalvikvm(23161): GC_BEFORE_OOM freed 10K, 9% free 59019K/64400K, paused 29ms, total 30ms
01-16 13:42:15.863: ERROR/dalvikvm-heap(23161): Out of memory on a 8018704-byte allocation.
01-16 13:42:15.863: ERROR/AndroidRuntime(23161): FATAL EXCEPTION: main
        java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
        at android.content.res.Resources.loadDrawable(Resources.java:1965)
        at android.content.res.Resources.getDrawable(Resources.java:660)
        at android.widget.ImageView.resolveUri(ImageView.java:616)
        at android.widget.ImageView.setImageResource(ImageView.java:349)
        at <MyApp>.MyActivity$6.run(MyActivity.java:143)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        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:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

Someone can give my some tips how to handle the crashes? Maybe its because my activitys are set to state "paused" instead of closing them correctly?

like image 469
Prexx Avatar asked Jan 16 '13 12:01

Prexx


People also ask

How do you handle bitmaps in Android as it takes too much memory?

Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.

How is a bitmap stored in memory?

The bitmap is stored in its original compressed form to save memory (PNG, JPEG, etc) The bitmap is stored in 24-bpp format so its slower to access than a 32-bpp image. The bitmap is not stored in a packed memory array and is fragmented so can't be read/written fast.

How do I reset my bitmap?

if you write bitmap. recycle() then it will release memory of bitmap, if you want to use again you have to create new one. Thanks!

What are bitmaps in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.


1 Answers

for quick fix you can add

android:largeHeap="true" in your manifest application tag

link here:

I face OOM problem for my table in kindle and nook

Heap size (large)
android:largeHeap="true"

there specification is mentioned to use larger heap link here:

edit: use Caching Bitmaps technique link here

like image 166
Padma Kumar Avatar answered Oct 21 '22 05:10

Padma Kumar