Is there a memory leak in remember function? Becau...
# compose
t
Is there a memory leak in remember function? Because in the following code finalize is never called? Or did i something wrong?
Copy code
data class Test(val pos: Int) {
    protected fun finalize() {
        log("Test finalized: $pos")
    }
}
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var pos by remember { mutableStateOf(0) }
            Box(Modifier.fillMaxSize().clickable { pos++ }) {
                val test = remember(pos) { Test(pos) }
                Text("${test.pos}")
                onCommit {
                    log("Test: $pos commited")
                    onDispose {
                        log("Test: $pos disposed")
                    }
                }
            }
        }
    }
}
s
why would it finalize?
t
When i click on the screen it should finalize because than the next Test object is loaded.
s
which click?
oo
ok sorry
t
The point of the code is to show that there is a memory leak in compose when remember gets a new pos value
s
possible. I would expect the old Test object to be unreferenced as well, but how do you force GC?
t
I do not. But i did tests with large image objects. And they live forever. Until outofmemory exception
I tested with compose for Desktop and just see that the same problem exists in compose for android
Btw. following code will call finalize:
Copy code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        repeat(10) {
            Test(it)
        }
    }
}
g
Wouldn't
remember(pos){}
basically store the objects for each different position? Meaning the runtime will "remember" it as nothing replaces it. I'm not sure if there is a way to dispose "remembered" values.
t
I would assume that when dispose is called that than all remembered values should be freed.
Ok i think it works. When i add
Copy code
.clickable { pos ++ ; System.gc() })
Finalized is getting called. Sorry