OutOfMemory Error when trying to convert string list to long list using type converter in room db

Viewed 33

I am getting the following error when I am using a type converter to convert a string array to long array and saving it into the room database.

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 2064 byte allocation with 1782896 free bytes and 1741KB until OOM, target footprint 268435456, growth limit 268435456; giving up on allocation because <1% of heap free after GC.
   at com.google.gson.stream.JsonReader.<init>(JsonReader.java:231)
   at com.google.gson.Gson.newJsonReader(Gson.java:767)
   at com.google.gson.Gson.fromJson(Gson.java:896)
   at com.google.gson.Gson.fromJson(Gson.java:846)
   at com.tigerit.signalmessenger.database.converters.DataConverter.toLongList(DataConverter.java:20)
   at com.tigerit.signalmessenger.database.daos.ConversationDao_Impl$50.call(ConversationDao_Impl.java:2063)
   at com.tigerit.signalmessenger.database.daos.ConversationDao_Impl$50.call(ConversationDao_Impl.java:1903)
   at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
   at java.lang.Thread.run(Thread.java:920)

Below is my type converter code,

class DataConverter {
@TypeConverter
fun fromLongList(value: List<Long>): String {
    val gson = Gson()
    val type = object : TypeToken<List<Long>>() {}.type
    return gson.toJson(value, type)
}

@TypeConverter
fun toLongList(value: String): List<Long> {
    val gson = Gson()
    val type = object : TypeToken<List<Long>>() {}.type
    return gson.fromJson(value, type)
}
}

When the type converter is trying to convert the string list from the DB to long list then the error happens. The string list can contain upto 200 items like the following,

[56,29,104,126,13,115,66,127,20,2,101,42,49,48,114,9,23,64,108,110,61,84,62,50,97,120,26,96,67,59,65,111,113,80,38,58,89,70,98,125,75,72,8,24,105,119,25,69,14,35,88,11,91,15,92,57,33,36,129,44,10,79,12,106,22,27,81,34,41,85,18,32,21,37,94,68,78,53,132,6,122,73,109,43,4,118,102,74,40,133,5,117,16,130,86,131,1,128,31,63,3,46,99,30,19,39]

0 Answers
Related