简体   繁体   中英

Android Robolectric: OutOfMemoryError - GC overhead limit exceeded, when Creating ArrayList

I want to create a test for a method that returns an ArrayList . The ArrayList type is a custom object called DateItem . But when I try to create the ArrayList in my test code (which is placed in test folder), the test failed with the following message:

java.lang.OutOfMemoryError: GC overhead limit exceeded

Process finished with exit code 255

Here's my code:

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

I am wondering how to create such ArrayList in my test code. I've looked into this answer but it is for the whole app, not only for testing purposes. How to allocate more memory for unit testing?

EDIT: After debugging, the code failed when in this line: val dateItem = DateItem(currentDate, ArrayList()) .

Actually you have endless WHILE loop because currentDate.plusDays(1) return copy of currentDate . Change to:

currentDate = currentDate.plusDays(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM