每当应用程序崩溃时,我在物理设备上运行我的应用程序。 LogCat显示“内存不足,无法分配带有16768392个空闲字节的74649612字节分配和直到OOM的49MB”
我使用的设备是小米RedMi note 4 3GB RAM Android版7.0处理器Qualcomm Snapdragon 625
这是LogCat消息
java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte allocation with 16768392 free bytes and 49MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:624)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:457)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
at android.content.res.ResourcesImpl.createFromResourceStream(ResourcesImpl.java:1272)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:743)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:585)
at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:308)
at android.content.res.Resources.getDrawable(Resources.java:785)
at android.content.res.Resources.getDrawable(Resources.java:756)
at com.suman.udit.su.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6852)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
OutOfMemoryError是android中最常见的问题,尤其是在处理位图时。当由于内存空间不足而无法分配对象时,Java虚拟机(JVM)会抛出此错误,并且垃圾收集器也无法释放一些空间。
正如Aleksey所提到的,您可以在清单文件android:hardwareAccelerated="false"
中添加以下实体,android:largeHeap="true"
它将适用于某些环境。
<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">
你一定要阅读一些Androids Developer的概念,特别是这里:qazxsw poi
阅读所有5个主题并重新编写代码。如果它仍然不起作用,我们将很高兴看到您在教程材料中做错了什么。
这里是SOF中这些类型错误的一些可能答案
Displaying Bitmaps Efficiently
Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap
How to solve java.lang.OutOfMemoryError trouble in Android
看起来你想在Solution for OutOfMemoryError: bitmap size exceeds VM budget加载的Drawable(图片,也许是?)太大了。您也可以尝试将其放在MainActivity.java:52
文件夹中,以便系统在加载时不会尝试重新缩放它。