Android:Java.Lang.Outofmemoryerror: Failed to Allocate a 23970828 Byte Allocation With 2097152 Free Bytes and 2Mb Until Oom

Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

OutOfMemoryError is the most common problem that occurs in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

As mentioned by Aleksey, you can add the below entities in your manifest file android:hardwareAccelerated="false" , android:largeHeap="true" it will work for some environments.

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

you should definitely read some of Androids Developer concept's, especially here:Displaying Bitmaps Efficiently

Read all 5 topics and rewrite your code again. If it still doesn't work we will be happy to see what you've done wrong with the tutorial material.

Here some of the possible answers for these type of errors in SOF

Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap

How to solve java.lang.OutOfMemoryError trouble in Android

Android : java.lang.OutOfMemoryError

java.lang.OutOfMemoryError

Solution for OutOfMemoryError: bitmap size exceeds VM budget

Edit: From the comments of @cjnash

For anyone that still had crashes after they added this line, try sticking your image into your res/drawable-xhdpi/ folder instead of your res/drawable/ and this should resolve this issue

Android Out of memory error with newNonMovableArray in loading Drawable

Thanks to @Umesh for reminding me for the question about the working solution:

Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

although i am not using a bitmap, all binary graphics must be include into drawable-xhdpi/ directory (or larger). So although I am not loading aný bitmap data (as kind of bmp files), the UI loads a binary image. Therefore it must pu put into the correct diectory, dependin its size (where binary images are always xhdpi or above).

java.lang.OutOfMemoryError - How do I fix this?

Try to load image using Glide or Picasso.

For Glide

into Project level build.gradle

dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:22.0.0'
}

while displaying image,

Glide.with(context)
.load("imageURL")
.into(ivImg);

For Piccaso

dependencies {
compile 'com.squareup.picasso:picasso:2.5.1'
}

while displaying image,

Picasso.with(context)
.load(imageURL)
.into(ivImg);


Related Topics



Leave a reply



Submit