7

My application crashes, showing this in logcat:

java.lang.OutOfMemoryError: (Heap Size=39047KB, Allocated=19932KB)
        at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
        at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:373)
        at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:443)
        at com.mApp.mobileapp.mActivity.onActivityResult(mActivity.java:196)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
        at android.app.Activity.dispatchActivityResult(Activity.java:4752)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3449)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3503)
        at android.app.ActivityThread.access$1100(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1320)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:5109)
        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:991)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
        at dalvik.system.NativeStart.main(Native Method)

While running this code:

String selectedImagePath = data.getStringExtra("imageByteCode");
try {
    File imageFile = new File(selectedImagePath);
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile
        .getAbsolutePath());//line 196
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    base64code = selectedImagePath;
    mImage.setImageBitmap(bitmap);
    } catch (Exception e) {
    }

Since i was catching it, i never expected it to crash the app. selectedImagePath is the path of the selected image on the SD card, which never exceeds 3m.b but Heap Size=39047KB, Allocated=19932KB ??

Thank You

2 Answers 2

11

You did not catch it. OutOfMemoryError is derived from Error, not from Exception. It is an Error because normally an application is not supposed to catch it, there is close to nothing your application can do to recover.

Sign up to request clarification or add additional context in comments.

2 Comments

great. But none of the images are more than 3mb in size so, i might not be handling bitmap memory allocation the right way?
For loading large images this may help: stackoverflow.com/questions/2798756/…
10

Place android:largeHeap="true" in your Manifest file under the application tag.

The largeHeap tells the VM to give you more RAM so you can manage big bitmaps and Strings. LargeHeap works in api 12+.

And if you want to catch the error with try/catch block you need to use

try
        {

        }catch (OutOfMemoryError e) {
            // TODO: handle exception
        }

If you are experiencing this while building the project in Android Studio

Error:Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded

add in your build.gradle file

dexOptions {
    javaMaxHeapSize "4g"
}

4 Comments

Epic Answer. A lot of information in as few words as possible. +1 largeheap. +1 OutOfMemoryError can be recovered from (sometimes) and this is the code to do it. In this situation the asker was trying to open an image. Using try catch(OutOfMemoryError) this won't crash the app and an error message can be displayed to the user.
@Andy I am glad that I helped.
I still need to use a better image caching library, but this gives me some leeway! thanks (going from loopj to Universal Image Loader)
android:largeHeap="true" is usually a bad idea, see here : stackoverflow.com/a/30930239/11278124

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.