android - Android 加载 Drawable 时 newNonMovableArray 出现内存不足错误

我的应用程序中有一个奇怪的(对我而言)“内存不足”错误。:

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

日志到此结束。对我来说,如果系统加载 Drwable 资源,就会发生错误。但除了一个 160kb 的大 png,如果只有矢量资源。

即使这个错误也只发生在真实设备上,而不是在具有相同规格的模拟器中。还有大约 350MB 的可用内存。虽然

android:largeHeap="true"

选项解决了问题(紧急方式),它似乎与堆上的某些东西有关。因此,如果没有任何内容,也会发生错误。只显示一个空片段。

任何想法如何深入研究问题?

此致。

回答1

感谢@Umesh 提醒我有关工作解决方案的问题:

https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio#answer-32245018

虽然我没有使用位图,但所有二进制图形都必须包含在 drawable-xhdpi/ 目录(或更大)中。因此,尽管我没有加载任何位图数据(作为 bmp 文件的一种),但 UI 会加载二进制图像。因此,它必须放入正确的字典中,具体取决于其大小(其中二进制图像始终为 xhdpi 或更高)。

相似文章