joseph_ivie
01/04/2024, 10:28 PMGC
to measure these things, but I'm almost certain it isn't working in my test case:
@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?joseph_ivie
01/04/2024, 11:30 PM