https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Pavel Repkin

03/10/2024, 6:28 AM
Hi! I'm making an iOS widget with KMM. iOS widget memory is limited at 30GB. Our widget crashes with an unexpected memory overrun because the memory is not released instantly. Here's a very simple test I've made to reproduce the problem. Kotlin class
Copy code
class Weather {
    var temperature: Float = 0f
    var conditions: String = ""
}
This test code is run on Swift widget launch. The array "a" is rewritten on each "j" iteration. But "a" memory is NOT released. The memory is getting piled up and a crash follows.
Copy code
var a = Array<Weather>()
for j in 1...1000 {
	a = Array<Weather>()
	for i in 1...10 * 1000 {
		a.append(Weather())
	}
	NSLog("\(j), m=\(MpSystem.shared.consumedMemoryMb())")
}
The output from the test code. The memory quickly runs away.
Copy code
1, m=11.844307
2, m=13.578682
3, m=15.344307
4, m=17.109932
5, m=18.859932
6, m=20.297432
7, m=21.953682
8, m=23.703682
9, m=25.469307
10, m=27.297432
11, m=29.016182
The widget crashes at this point with the following message. Thread 1: EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=30 MB) The crash is only observed on an actual device. There's no crash if Weather class created in Swift. It only crashes with classes from Kotlin. I have tried the following actions, but they didn't help at all. 1. Add this in gradle.properties
Copy code
kotlin.native.binary.mimallocUseCompaction=true
2. Switch back to the standard memory model with
Copy code
listOf(
        iosX64(),
        iosArm64(),
        //iosSimulatorArm64() sure all ios dependencies support this target
    ).forEach {
        it.compilations.configureEach {
            compilerOptions.configure {
                freeCompilerArgs.add("-Xallocator=std")
            }
        }
        it.binaries.framework {
//...
I wonder if KMM team is aware of this issue? Is there a workaround? Will this memory issue to be addressed in 2.0? Configuration. kotlin version: 1.9.20 iOS 16.7.5 Real device - iPhone 10
Anyone? 😞
10 Views