Android Robolectric:OutOfMemoryError - 创建ArrayList时超出GC开销限制

问题描述 投票:1回答:1

我想为返回ArrayList的方法创建一个测试。 ArrayList类型是一个名为DateItem的自定义对象。但是当我尝试在我的测试代码(放在ArrayList文件夹中)中创建test时,测试失败并显示以下消息:

java.lang.OutOfMemoryError: GC overhead limit exceeded

Process finished with exit code 255

这是我的代码:

var expectedDateItems: ArrayList<DateItem> = ArrayList()
val currentDate = date1Start
while (currentDate.isBefore(date1End)) {
    val dateItem = DateItem(currentDate, ArrayList())
    expectedDateItems.add(dateItem)
    currentDate.plusDays(1)
}

我想知道如何在我的测试代码中创建这样的ArrayList。我已经研究了this answer,但它适用于整个应用程序,不仅用于测试目的。如何为单元测试分配更多内存?

编辑:调试后,代码在此行中失败:val dateItem = DateItem(currentDate, ArrayList())

android arraylist kotlin garbage-collection robolectric
1个回答
3
投票

实际上你有无尽的WHILE循环,因为currentDate.plusDays(1)返回copycurrentDate。改成:

currentDate = currentDate.plusDays(1)
© www.soinside.com 2019 - 2024. All rights reserved.