kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Serialization of generic classes in Kotlin/Native causes memory leaks

Open darvld opened this issue 4 years ago • 9 comments

Any generic class instance, even one with a primitive type parameter, causes a memory leak when it is serialized. Edit: the same thing occurs both with Json and ProtoBuf formats, I don't know if it's related to the formatters or the serialization phase itself.

To Reproduce


@Serializable
public data class Data<T>(
    val a: T,
    val b: Boolean
)

public fun main(){
    val a = Json.encodeToString(Data(4))
}

Expected behavior Running the above produces the following message:

Memory leaks detected, 1 objects leaked!
Use `Platform.isMemoryLeakCheckerActive = false` to avoid this check.

Environment

  • Kotlin version: [1.4.20]
  • Library version: [1.4.20]
  • Kotlin platforms: [Native]
  • Gradle version: [e.g. 6.5.1]
  • IDE version: IntellijIDEA 2020.3
  • Platform: Windows 10 x64

darvld avatar Dec 31 '20 19:12 darvld

I am facing similar issue while running on iOS platform inside decodeFromString() function.

vpadmnabh avatar Jan 14 '21 14:01 vpadmnabh

Any updates on this yet?

darvld avatar Feb 19 '21 19:02 darvld

Any update on the leak?

KevinBassaDevelopment avatar Aug 26 '21 12:08 KevinBassaDevelopment

Not yet. Is it a blocker?

sandwwraith avatar Aug 31 '21 22:08 sandwwraith

Currently its rated as blocker for the future release of the application, as it must not contain memory leaks.

KevinBassaDevelopment avatar Sep 07 '21 11:09 KevinBassaDevelopment

I am facing a huge memory leak in iOS also when doing Json.encodeToString(Data(4)). I have an object class with a method that does manipulate JsonObjects. As soon as it gets called, simply goes up to 500MB and never frees up.

How can I force this to be freed? Should I use WeakReference?

kotlinx.serialization: 1.5.0

checoalejandro avatar May 25 '23 04:05 checoalejandro

We are on it; but it might be the case the fix requires a compiler version update though

qwwdfsad avatar May 25 '23 08:05 qwwdfsad

Thanks for the update, if anything helps, I was sending a big String to be serialized to a JsonElement (JsonObject). Many items were sent as such, making memory to go high. These were used through utils inside an object. Is there any recommendation to help the memory model to free up some of this? Any workaround to avoid as much as possible this retention?

Thanks.

checoalejandro avatar May 25 '23 16:05 checoalejandro

reified ? in inline function

app-z avatar Oct 31 '23 11:10 app-z

I wasn't able to reproduce this leak. Does anyone have an iOS project that demonstrates the issue?

sandwwraith avatar Apr 17 '24 17:04 sandwwraith

The original issue was found long before the new memory manager for Kotlin/Native was available. I haven't been able to reproduce it with modern versions, so I'll close this. Feel free to reopen if any new reproducers are found.

darvld avatar Apr 25 '24 18:04 darvld