Search code examples
arrayskotlinbitmapout-of-memoryandroid-bitmap

Gson toJson convert OutOfMemory(OOM)


val gson = GsonBuilder().setPrettyPrinting().create()

Log.e("surveyListPretreatment",gson.toJson(
    SurveyTab(viewModel.owner.treat).apply {
        surveyListPretreatment = viewModel.surveyTabs.surveyListPretreatment
    })
)

java.lang.OutOfMemoryError: Failed to allocate a 150994952 byte allocation with 25165824 free bytes and 103MB until OOM, max allowed footprint 118228816, growth limit 201326592

Because,

the DTO has

var imgByteArray:ByteArray? = null //bitmap convert byteArray => It's OOM Generated...

how can I fix that?


Solution

  • Jake Wharton already commented about this on GitHub Issues.

    There's no magic fix. You either need a larger heap or a smaller object.

    If you can do make your object smaller, it will be the most optimal solution. If your app really needs that much memory, you can try setting android:largeHeap="true" for your application in the manifest.

    Reference:

    1. http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap
    2. Out of memory exception in gson.fromJson()