欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决“java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM”错误

程序员文章站 2022-06-13 21:16:31
...

 “java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM”内存溢出错误是在Android app运行的时候出现的,这可能是因为app RAM中缓存太多文件或反复实例化相同的位图和视频造成的内存不足现象,下面是我在运行时候所出现的错误。

E/art: Throwing OutOfMemoryError "Failed to allocate a 6801708 byte allocation with 4172944 free bytes and 3MB until OOM"
D/skia: --- allocation failed for scaled bitmap
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.Login, PID: 23744
                  java.lang.OutOfMemoryError: Failed to allocate a 6801708 byte allocation with 4172944 free bytes and 3MB 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:988)
                      at android.content.res.Resources.createFromResourceStream(Resources.java:2857)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2547)
                      at android.content.res.Resources.loadDrawable(Resources.java:2447)
                      at android.content.res.VivoResources.loadDrawable(VivoResources.java:220)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                      at android.widget.ImageView.<init>(ImageView.java:146)
                      at android.widget.ImageView.<init>(ImageView.java:135)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
                      at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                      at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
                      at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:851)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:510)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:420)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:371)
                      at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                      at com.example.XunLian.SocialBehavior_First.First_ThrowPaperActivity.onCreate(First_ThrowPaperActivity.java:32)
                      at android.app.Activity.performCreate(Activity.java:6083)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
                      at android.app.ActivityThread.access$900(ActivityThread.java:175)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5418)
                      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:1037)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

 可以在androidmenifest.xml文件中更改application配置,将hardwareAccelerated设置为false,largeHeap设置为true。

<application
    android:allowBackup="true"
    android:icon="@mipmap/theme"
    android:label="来自星星的你"
    android:roundIcon="@mipmap/theme"
    android:supportsRtl="true"

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

    android:theme="@style/Theme.AppCompat.NoActionBar">

android:hardwareAccelerated = "true"的时候会牺牲内存来加快图片的加载速度。

android:largeHeap = "false"会限制app应用申请的内存大小。