0

I am facing this error :

FATAL EXCEPTION: main Process: com.petyaar, PID: 18056 java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 16777024 free bytes and 40MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:639) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:615) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:419) at com.petyaar.OwnerBio.OwnerBioUpdate.onCaptureImageResult(OwnerBioUpdate.java:611) at com.petyaar.OwnerBio.OwnerBioUpdate.onActivityResult(OwnerBioUpdate.java:643) at android.app.Activity.dispatchActivityResult(Activity.java:6508) at android.app.ActivityThread.deliverResults(ActivityThread.java:3702) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3749) at android.app.ActivityThread.access$1400(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

I don't know how to resolve this, also I have been monitoring my memory allocation in "Android Monitor" . The memory allocation keeps on increasing even when I am not interacting with the app . It goes to as high as 500MB+.

This is what I am doing with my image captured through camera .

 private void onCaptureImageResult(Intent data) {

    Bitmap thumbnail = null;


    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
            null, null);
    int column_index_data = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    //THIS IS WHAT YOU WANT!
    String capturedImageFilePath = cursor.getString(column_index_data);

    filename = capturedImageFilePath.substring(capturedImageFilePath.lastIndexOf("/") + 1);
    thumbnail = BitmapFactory.decodeFile(capturedImageFilePath);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, bytes);

    thumbnail=Bitmap.createScaledBitmap(thumbnail, 200, 300, true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Log.i("final camera size", String.valueOf(thumbnail.getAllocationByteCount()));
    }
    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");
    byte[] byteArray = bytes.toByteArray();
    encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
    Log.e("base64string name", encoded);

    Log.e("Image name", capturedImageFilePath);
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    profile_image.setImageBitmap(thumbnail);
}

Please tell me how to resolve this issue.

rajat44
  • 4,941
  • 6
  • 29
  • 37
  • In the AndroidManifest.xml file in the application tag put this tag android:largeHeap="true" – Sagar Gangawane Mar 20 '17 at 11:33
  • Why this ? As much as I have read , there are other problems as well in android:largeHeap="true". and what is the reason for continuously increasing memory size ? – rajat44 Mar 20 '17 at 11:35
  • Reason of large heap [check this link](http://stackoverflow.com/questions/27396892/what-are-advantages-of-setting-largeheap-to-true) – Sagar Gangawane Mar 20 '17 at 11:38
  • OutOfMemory does not always mean your app have Memory Leak, it could also mean that you might be needing a larger size more than the given allocation (Let's assume that no leak here). This error is typical in Android specially if you are setting **huge image** in a small `ImageView` or **small image** on Large `ImageView` because the conversion are causing that much consumption. – Enzokie Mar 20 '17 at 11:52
  • Using Picasso or Glide will help in loading images and does the caching behind the scene. – Enzokie Mar 20 '17 at 11:59

1 Answers1

2

Leakcanary is your good friend for memory leaks. It's really easy-to-use library that will help you to find memory leaks in your app. In your case, it seems that it's not a memory leak, but just some loop that is constantly allocating memory. Anyway, try Leakcanary. If it doesn't help, try finding which part of the app is taking all the memory using heap dump. More about it can be found here.

In your case, it seems that it's related to some work that you are doing on Bitmaps. Be sure that you are releasing them from memory. Also, keep in mind that images take memory in Android based on their size in pixels, and not based on their size in MB. So really simple png that is 10000x10000 but is only 1MB will take much more memory than 1000x1000 jpeg that is 3MB.

Vladimir Jovanović
  • 2,261
  • 2
  • 24
  • 43
  • I have created the .hprof file , but I don't know how to use that for getting the memory leaks. Can you guide me for the same? – rajat44 Mar 20 '17 at 11:57
  • Heap dump is basically all memory that your app allocated. There you can see which objects are taking most of the memory, and base on that find part of the app that has bug. If it's not a big app you can paste parts of the code where you are doing some heavy lifting (image processing, work on big text files etc.) – Vladimir Jovanović Mar 20 '17 at 12:05
  • I have installed LeakCanary ,but it didn't show any notification when my app crashed due to outOfMemory exception . And what steps should I follow when I get to know the line number of the error. For ex - in one of my logs I am getting error in setContentView(layout) . What can I get from this error ? – rajat44 Mar 20 '17 at 20:13
  • What are you doing with images? It seems that you use big images or a lot smaller images. Can you paste the code where you handle Bitmaps? – Vladimir Jovanović Mar 20 '17 at 20:17
  • I suppose that image that you are trying to load is really big. If so, then problem is in this line `BitmapFactory.decodeFile(capturedImageFilePath)`. You shouldn't load a full sized image into memory. Read more about handling big images [here](https://developer.android.com/topic/performance/graphics/load-bitmap.html). – Vladimir Jovanović Mar 21 '17 at 20:09