'Android Out of memory error with newNonMovableArray in loading Drawable

I've got a strange (for me) "Out of memory" error in my app.:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.fhkiel.socialrobotcms, PID: 5029
java.lang.OutOfMemoryError: Failed to allocate a 136887708 byte allocation with 3292628 free bytes and 123MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:651)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1085)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2867)
    at android.content.res.Resources.loadDrawable(Resources.java:2756)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
    at android.widget.ImageView.<init>(ImageView.java:152)
    at android.widget.ImageView.<init>(ImageView.java:140)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:73)
    at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:206)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:125)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1566)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1617)
    at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at androidx.fragment.app.Fragment.onCreateView(Fragment.java:1968)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1374)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2841)
    at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2777)
    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3020)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
    at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1374)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2841)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1245)
    at android.app.Activity.performStart(Activity.java:6355)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2540)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5875)
    at jav

here the log ends. For me the errors semms to happen, if the system loads a Drwable resource. But besides a 160kb big png, if have only vector assets.

Even this error only happens on a real device, not in the emulator with the same specs. Also there are around 350MB of free memory. While the

android:largeHeap="true"

option fixes the problems (the urgly way), it seems to have to do with something on the heap. therefore the error also happens, if the doesn nothing. Just displaying one empty fragment.

Any ideas how to dig deeper into the problem?

best regards.



Solution 1:[1]

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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 k1ngarthur