Jasjeet Singh
08/08/2024, 9:06 AMfun myFun() {
var x = 0
someObject.addCallback(object: Callback() {
override fun first(value: Int) {
x = value // Properly referenced object as Int is a wrapper
}
override fun second(obj: Obj) {
// I use the reference to x here.
obj.set(x) // pass by copy is what I would assume if it was not a wrapper.
}
})
}
When this function is called in any of the activity's function, lets say onCreate
here, now the question is that when the callback executes in future and changes the value of x, is there any chance that the GC might in between the declaration and when the callback is run, hence clearing x
and causing a memory leak?
From my understanding, Int
is not a primitive but a wrapper around java int
primitive for either JVM or ART, making GC able to collect it and hence this question.