Hi <@UHAJKUSTU> when i do `navigation.pop()` it...
# decompose
s
Hi @Arkadii Ivanov when i do
navigation.pop()
it seems that the popped component is not released from memory as on popping the memory usage is not freed up, rather if i keep pushing and popping alternately memory usage keeps increasing in ios app.
a
I'm not sure, but on Kotlin/Native the GC may be running later when needed. Could this be the case? Maybe try triggering GC explicitly and check if the memory goes down?
Also worth checking the UI, maybe there are some kind of cycling references.
s
Memory is not released till minutes after popping
a
If the memory is released eventually, then this is the K/N behaviour.
You can try manually without Decompose. Try allocating an array (like 1Kb) and remember to a variable, then assign null.
s
Ok will check and share more insights
👍 1
a
There is some information in the docs: https://kotlinlang.org/docs/native-memory-manager.html
E.g. it says that memory collection is executed periodically.
s
Check this recording of memory usages @Arkadii Ivanov
a
What if instead of navigation, you will do the following in Kotlin on every button click: fun foo() { val x= ByteArray(1000000) pintln(x.size) }
s
ok
Copy code
private fun onProjectListingOutput(output: ProjectListingComponent.Output): Unit =
        when (output) {
            is ProjectListingComponent.Output.NavigateBack -> navigation.pop()
            is ProjectListingComponent.Output.NavigateToProjectDetails -> {
                navigation.push(Configuration.ProjectDetails(projectId = output.projectId))
            }

            is ProjectListingComponent.Output.Logout -> {
                navigation.replaceAll(Configuration.EnterMobileNumber)
            }

            is ProjectListingComponent.Output.NavigateToNotification -> {
                foo()
//                navigation.push(Configuration.Notification)
            }
        }

    fun foo() {
        val x = ByteArray(1000000)
        println(x.size)
    }
done
each time i click 1MB memory usage increases
a
On the video, I can see that the memory goes a bit up when you return back to the My Projects screen. When you push Notifications, it goes a bit down, and then again up when you pop to My Projects. This doesn't look like a memory leak. Worth checking what actually uses consumes the memory in My Projects screen
s
like it went from 165 to 170Mb then didnt increases
a
Well, the video doesn't indicate any memory leaks. It could be your memory cache consuming the memory (if any), repositories, etc. Also make sure you don't accumulate any data every time My Project screen resumes. On my side, I will check the Decompose sample on iOS.
s
check at 2:05 minutes of video, even when i click on notification and go to detail screen and then come back to notification memory usga ejumps up again , indicating that there is no issue in My Projects screen as it is the same case with Notification screen @Arkadii Ivanov
a
At 2:05 you are going deeper to a notification item screen. Then when you go back to notifications, the screen clearly does something, and the memory increases. That's my observation.
I will provide a way how you could check whether a class is deallocated or not, a bit later.
s
yes, please. THanks
a
Re: deallocation checker In commonMain:
Copy code
internal expect fun Any.nativeDeallocChecker(): Any
In iosMain:
Copy code
import kotlin.native.internal.createCleaner

@OptIn(ExperimentalStdlibApi::class)
internal actual fun Any.nativeDeallocChecker(): Any {
    println("Allocated: $this")

    return createCleaner(argument = this) { println("Deallocated: $it") }
}
Then inside a component class:
Copy code
class DefaultSomeComponent : SomeComponent {
    private val marker = listOf("Counter")
    private val deallocChecker = marker.nativeDeallocChecker()

    // The reset of the code
}
It is important that the checker must not reference the component itself, it should only reference the marker, otherwise it may not work. Once the component is deallocated, the marker object is also deallocated and the checker will print the message. I have just checked on the Decompose sample, all
DefaultCounterComponent
instances are deallocated eventually after pop. Not immediately, but eventually after some time. In my case it is ~10 seconds.
s
@Arkadii Ivanov Thanks, will test tomorrow and share the the insights. Thanks
👍 1