kotlinx.serialization
kotlinx.serialization copied to clipboard
Serialization of generic classes in Kotlin/Native causes memory leaks
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
I am facing similar issue while running on iOS platform inside decodeFromString() function.
Any updates on this yet?
Any update on the leak?
Not yet. Is it a blocker?
Currently its rated as blocker for the future release of the application, as it must not contain memory leaks.
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
We are on it; but it might be the case the fix requires a compiler version update though
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.
reified ? in inline function
I wasn't able to reproduce this leak. Does anyone have an iOS project that demonstrates the issue?
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.