Android抛出OutOfMemoryError "Failed to allocate a 164 byte allocation with 44 free bytes and 44B until OOM"(递归情况)

15

这个应用昨天还可以用,但今天启动时出现以下错误:

Throwing OutOfMemoryError "Failed to allocate a 164 byte allocation with 44 free bytes and 44B until OOM" (recursive case)

我不知道发生了什么,也不知道为什么会出现内存不足的情况。这只是模拟器不完美工作吗?还是与我的应用程序有关?

我不知道你需要多少信息。如果需要更多信息,请告诉我,我会提供。

更新:

我猜这与垃圾收集器有关?请参见下文

Background sticky concurrent mark sweep GC freed 2839(223KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 1603KB/1603KB, paused 31.379ms total 44.794ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 529(29KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 1574KB/2MB, paused 52.919ms total 60.062ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 5551(277KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 4MB/7MB, paused 15.550ms total 85.518ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 20777(1035KB) AllocSpace objects, 0(0B) LOS objects, 36% free, 7MB/11MB, paused 12.467ms total 61.496ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 46653(2MB) AllocSpace objects, 0(0B) LOS objects, 24% free, 12MB/16MB, paused 1.335ms total 138.439ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 27622(1368KB) AllocSpace objects, 0(0B) LOS objects, 20% free, 15MB/19MB, paused 1.459ms total 182.567ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 45635(2MB) AllocSpace objects, 0(0B) LOS objects, 18% free, 17MB/21MB, paused 1.365ms total 157.690ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 22802(1159KB) AllocSpace objects, 0(0B) LOS objects, 16% free, 20MB/24MB, paused 1.211ms total 180.755ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 14475(641KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 23MB/24MB, paused 1.106ms total 122.625ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 27477(1338KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 23MB/27MB, paused 1.511ms total 147.653ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 37938(1865KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 25MB/29MB, paused 1.253ms total 178.420ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 23214(1147KB) AllocSpace objects, 0(0B) LOS objects, 12% free, 28MB/32MB, paused 3.789ms total 190.825ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 36253(1773KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 31MB/35MB, paused 1.085ms total 227.583ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 13949(616KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 34MB/35MB, paused 665us total 175.362ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 40440(2000KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 33MB/37MB, paused 2.158ms total 311.515ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 13956(617KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 36MB/37MB, paused 42.198ms total 81.851ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 33376(1624KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 36MB/40MB, paused 1.211ms total 322.374ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 4052(174KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 40MB/40MB, paused 1.245ms total 105.928ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 38452(1891KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 39MB/43MB, paused 1.212ms total 282.053ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 15083(667KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 42MB/43MB, paused 1.006ms total 134.368ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 33122(1609KB) AllocSpace objects, 0(0B) LOS objects, 8% free, 42MB/46MB, paused 1.759ms total 685.984ms
2431-2443/com.example.christian.welldanaapp W/art﹕ Suspending all threads took: 21.776ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 2898(122KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 46MB/46MB, paused 23.019ms total 71.640ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 29828(1485KB) AllocSpace objects, 0(0B) LOS objects, 8% free, 45MB/49MB, paused 1.250ms total 194.222ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 37388(1848KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 48MB/52MB, paused 1.243ms total 332.837ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 31151(1528KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 51MB/55MB, paused 1.446ms total 336.916ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 12755(563KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 54MB/55MB, paused 14.512ms total 48.881ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 40981(2014KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 53MB/57MB, paused 1.288ms total 402.576ms
2431-2443/com.example.christian.welldanaapp W/art﹕ Suspending all threads took: 18.769ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background partial concurrent mark sweep GC freed 32370(1572KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 56MB/60MB, paused 20.234ms total 387.007ms
2431-2443/com.example.christian.welldanaapp W/art﹕ Suspending all threads took: 10.568ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Background sticky concurrent mark sweep GC freed 12559(558KB) AllocSpace objects, 0(0B) LOS objects, 1% free, 59MB/60MB, paused 11.757ms total 42.520ms
2431-2443/com.example.christian.welldanaapp I/art﹕ Clamp target GC heap from 64MB to 64MB

实际的红色文字

07-22 06:39:06.976    2431-2431/com.example.christian.welldanaapp E/AndroidRuntime﹕ Error reporting crash
java.lang.OutOfMemoryError: Failed to allocate a 458 byte allocation with 1613776 free bytes and 1575KB until OOM; failed due to fragmentation (required continguous free 32768 bytes for a new buffer where largest contiguous free 4096 bytes)
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)
        at java.lang.StringBuilder.append(StringBuilder.java:216)
        at java.lang.Throwable.toString(Throwable.java:360)
        at java.lang.Throwable.printStackTrace(Throwable.java:315)
        at java.lang.Throwable.printStackTrace(Throwable.java:300)
        at android.util.Log.getStackTraceString(Log.java:335)
        at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59)
        at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43)
        at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

编辑:Java 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ChangeLanguage("dk");
    SetListeners();
}

//Changes the language depending on the button pressed
private void ChangeLanguage(String language){
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config,getResources().getDisplayMetrics());
    setContentView(R.layout.activity_main);
    RestartActivity();
}

//Restarts the activity after changing the languagse
private void RestartActivity(){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

1
你的应用程序中使用位图吗? - Oli
1
@Oli 图片?是的 android:background="@drawable/backgroundimage" - Detilium
1
尝试在AndroidManifest.xml文件中的应用程序标签中添加android:largeHeap="true" - Oli
backgroundimage 的大小是多少 KB,分辨率是多少?此外,你把这个文件放在哪个 /drawable 文件夹里了? - Awanish Raj
1
我刚刚看到了你的堆栈跟踪。此外,请在此处发布相关的Java代码!问题似乎不在图像中,而是在某个地方构建字符串! - Awanish Raj
显示剩余7条评论
5个回答

7
尝试这个方法,可能会帮助您在清单文件中添加此标签。
<application android:largeHeap="true">
</application>

它将为您的应用程序分配大量堆空间。祝你好运:)


谢谢您的建议。虽然它通常可以帮助避免内存崩溃,但我在这里仍然遇到了错误。而且,正如其他评论中提到的那样,我确实有一张非常大的图片。 - jeevium

6

在发生OutOfMemory异常的情况下,最常见的问题是图像大小超过了最大值。

您可以调整图像大小,例如:

 Picasso.with(context)
                            .load(new File(arrayList.get(position).getUrl()))
                            .networkPolicy(NetworkPolicy.NO_CACHE)
                            .memoryPolicy(MemoryPolicy.NO_CACHE)
                            .resize(300,300)
                            .error(R.drawable.placeholder_full_image)
                            .placeholder(R.drawable.placeholder_full_image)
                            .into(genericViewHolder.productImage_iv);

这对我来说是有效的


6
这种错误通常发生在项目中使用了非常大的图片时。尝试缩小您的图片,这个错误就会被解决。
如果您没有使用像Photoshop或Illustrator这样的专业图像编辑软件,您可以使用内置的图像查看器完成这个简单的任务。
在Mac上,您可以使用预览应用程序来缩小您的图像。
  • 使用预览打开您的图像(双击图像即可,预览是默认应用程序)。
  • 选择标记工具栏(看起来像一个公文包)。
  • 点击调整大小按钮(看起来像一个正方形,其对角线上有两个箭头)。
  • 输入所需的宽度和高度。
  • 点击“确定”,您就完成了 =)

** 我不使用PC,所以无法解释如何在PC上操作,但这也应该非常简单。

希望能对某些人有所帮助...

1
它实际上是 Stack Overflow 上唯一帮助我解决这个问题的答案,谢谢! - Mr. Xcoder

4

你的应用程序进入了无限循环,导致内存耗尽。请尝试以下步骤: onCreate->ChangeLanguage->RestartActivity->onCreate->ChangeLanguage->RestartActivity...

你需要结束它。


4

OutOfMemoryError: 分配458字节内存失败,当前有1613776字节和1575KB可用内存,但由于碎片化无法继续分配;因为需要32768字节的连续空闲内存块来创建新缓冲区,而最大的连续空闲内存块只有4096字节。

应用程序尝试从剩余的1613776字节中分配458字节的内存,但由于碎片化,它无法找到32768个连续的空闲字节来创建新的缓冲区。

这篇文章解释了这是由于操作系统内存管理不佳导致的。我在安卓5和6的手机上遇到过这个问题,我认为作为开发者我们无法做出修复。


文章链接已失效。不过,我会点赞你的评论,因为它确实发生在一个Android 5设备上。如果您能分享另一个链接,并解释一下这个问题,那将非常有用。 - xarlymg89
2
好在我们有网络档案,可以查看https://web.archive.org/web/20180429154436/http://rebrn.com/re/poor-ram-management-affecting-the-galaxy-s-and-s-edge-189814/,这篇与程序相关的内容。 - Goran Horia Mihail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接