trustwallet / wallet-core

Cross-platform, cross-blockchain wallet library.
https://developer.trustwallet.com/wallet-core
Apache License 2.0
2.87k stars 1.61k forks source link

[KMP] Memory leak found in PublicKey #4096

Open 10gic opened 1 week ago

10gic commented 1 week ago

Describe the bug I found a memory leak in PublicKey within the Kotlin package of Wallet Core.

To Reproduce The following code can trigger a memory leak:

println("Test started")
val publicKeyHex =
    @Suppress("ktlint:standard:max-line-length")
    "044ba28b11af1561042b03b9d0f940446315af11358aa12d798050b3cf76265dab0f48b22ea1fc1f9560c969b966221f2821b746c4e56efaeaeec8689caf5843c9"
var i = 1
val publicKeyByteArray = publicKeyHex.hexToByteArray()
while (i <= 10_000_000) {
    val publicKey = PublicKey(publicKeyByteArray, PublicKeyType.SECP256k1Extended)
    if (i % 20_000 == 0) {
        println("Test case $i run")
    }
    i++
}
println("Test completed")

Expected behavior No memory leak.

Screenshots Memory Profiler Screenshot:

kmp-memory-leak-public-key

Additional context

satoshiotomakan commented 1 week ago

Hi @10gic, this issue duplicates https://github.com/trustwallet/wallet-core/issues/4021 As I mentioned before, GC doesn't have time to trigger the ReferenceQueue to clean up the memory. But in real life there shouldn't be the memory leak. However, if there is something I'm wrong with, or miss something, I'd be happy to know

10gic commented 1 week ago

The root cause is completely different, so I opened a new bug.

satoshiotomakan commented 1 week ago

I see @10gic, thanks. I'm investigating the bug, thank you!