rajat44
rajat44

Reputation: 5121

How to check memory leaks ? and how to tackle those?

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.

Upvotes: 0

Views: 430

Answers (1)

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

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.

Upvotes: 2

Related Questions