OutOfMemoryError Failed to allocate

Today we’ll talk about OutOfMemoryError in android studio.

This type of error is caused by Bitmap, so you can solve it in many ways, but we also use a simple method to solve every Android Studio error.

So friends if you are getting following type of error while compiling your project then you can easily solve it.

If you are getting following type of error during compilation, then solve it easily.

What happens when you compile your project?

When you run your project on Android, that time the studio app crashes. And when you see log cat then you get errors like below.

 OutOfMemoryError "Failed to allocate a 17160972 byte allocation with 9286528 free bytes and 8MB until OOM"
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2579)
        at android.content.res.Resources.loadDrawable(Resources.java:2468)
        at android.content.res.Resources.getDrawable(Resources.java:801)
        at android.content.Context.getDrawable(Context.java:402)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:455)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:147)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:135)
        at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
        at androidx.appcompat.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:63)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:82)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:69)
        at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:199)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:119)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1563)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1614)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:6008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5240)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

According to the Android developer, “Thrown when the java virtual machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. outOfMemoryError objects may be constructed by the virtual machine as if”

In simple

Here, used a lot of images, and made an app. The object unable to allocate the lack of memory space eventually caused the Java Virtual Machine to throw an OutOfMemory error like this.

How to solve OutOfMemoryError out of memory error in Android Studio?

Let’s try to solve OutOfMemoryError error in Android Studio.

Also Read: how to share app link in android

We will discuss step by step so that you can understand easily.

Go to AndroidManifest.xml

App ⇾manifests⇾ AndroidManifest.xml

Add below entities.

android:hardwareAccelerated="false"
android:largeHeap="true"

Full code of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <application
        android:largeHeap="true"
        android:hardwareAccelerated="false"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name="com.example.myapplication">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
    </application>
</manifest>

Now, run your project. It worked.

Leave a Reply