Having some trouble with a memory leak. It mentio...
# kotlin-native
j
Having some trouble with a memory leak. It mentions at https://kotlinlang.org/docs/native-memory-manager.html#check-for-memory-leaks that we can use
GC
to measure these things, but I'm almost certain it isn't working in my test case:
Copy code
@OptIn(ExperimentalForeignApi::class)
class CustomUIView: UIView(CGRectMake(0.0, 0.0, 0.0, 0.0)) {
}

@OptIn(ExperimentalStdlibApi::class, NativeRuntimeApi::class)
fun getUsage(): Long {
    repeat(5) {
        println("Collecting...")
        GC.collect()
        sleep(1u)
    }
    println("Ready")
    return GC.lastGCInfo!!.memoryUsageAfter["heap"]!!.totalObjectsSizeBytes
}

fun shouldNotAllocPermanently() {
    val v = CustomUIView()
    val sub = CustomUIView()
    v.addSubview(sub)
}

fun runTest() {
    val start  = getUsage()
    repeat(100_000) {
        shouldNotAllocPermanently()
        if(it % 10_000 == 0) print('.')
    }
    val end = getUsage()
    println(end - start)
}
This doesn't appear to work as a way to check for memory leaks. What happens is this: •
runTest()
's loop starts and memory usage climbs sharply. • The loop ends and the first
GC.collect
freezes the system for a bit, but doesn't free up any significant memory. •
runTest()
completes. After a few minutes, a real GC seems to start and most of that memory is freed. In addition, it seems even after that it's still leaking some memory in this circumstance. Anyone have some insight?
Happens on latest stable and on K2-beta2