Hi everyone :wave: I recently upgraded my Kotlin M...
# multiplatform
c
Hi everyone 👋 I recently upgraded my Kotlin Multiplatform project from Kotlin 1.9.0 to Kotlin 2.x (to take advantage of some newer features and library compatibility). The shared code targets iOS, Android, and web. After the upgrade, my iOS app started to lag or even freeze for tens of seconds, during which Instruments shows the Kotlin GC (ConcurrentMarkAndSweep) taking huge amounts of CPU time:
Copy code
std::__1::invoke_result<kotlin::gc::ConcurrentMarkAndSweep...
kotlin::gc::mark::ParallelMark::parallelMark(kotlin::ParallelProcessor...
    Kotlin_processObjectInMark
    Kotlin_processArrayInMark
I see the memory usage spike, and the app is stuck until the GC finishes scanning. On Kotlin 1.9 the app is super smooth. One code change we made was removing explicit
freeze()
calls in a small Flow wrapper. For example, we used to do:
Copy code
@OptIn(FreezingIsDeprecated::class)
class FlowWrapper<out T>(...) {
  init { freeze() }
  ...
}
That’s gone because freeze is deprecated under the newer memory model. But I’m wondering if that might affect how large object graphs are pinned or scanned. Has anyone run into big GC stalls on iOS with Kotlin 2.x? If so, how did you handle it? Any help or tips would be awesome. Thanks!
s
Hey! What exact Kotlin version do you use?
c
Hi ! I tried with many different versions but none seems to work so I am sticking with the latest ones now;
2.1.10
- Also updated to the latest kotlinx-coroutines
1.10.1
and kotlinx-serialization-json
1.8.0
What seems to have worked though, not sure which change it was, but for anyone looking at this thread: • + adding this new property on gradle.properties
kotlin.native.binary.gc=cms
• - removing this old one
kotlin.native.binary.memoryModel=experimental
• Explicitly setting to
null
all the big classes that took a long time to garbage collect from the Xcode Instruments profiler tools
s
Glad to hear that!
• - removing this old one
kotlin.native.binary.memoryModel=experimental
This should not have any effect.
+ adding this new property on gradle.properties
kotlin.native.binary.gc=cms
Yep, CMS should help with GC pauses. Though, I would be surprised if it makes that much difference :)
• Explicitly setting to
null
all the big classes that took a long time to garbage collect from the Xcode Instruments profiler tools
That's an interesting idea! Does instances of these classes "cross" language border between Kotlin and Swift/Objective-C, and/or have properties that store Swift/Objective-C objects?
c
Good to know ! This instances in particular never "cross" languages, all stays in Kotlin.
👍 1