-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[KMP] Memory leak found in AnyAddress #4021
Comments
Hi @10gic, good catch! I'll investigate what can be the reason for the memory leak. |
Hi @satoshiotomakan, I found that this is a common issue. It exists in HDWallet, PrivateKey, PublicKey, and others. For swift binding (swift/Sources/Generated/AnyAddress.swift), we have deinit code to release memory:
For wasm binding (wasm/src/generated/AnyAddress.h), we have destructor to release memory:
Kotlin don't support destructor, we need to think of other solutions to release memory. |
Hi @satoshiotomakan, do you have any ideas on how to fix this issue (#4021)? |
@10gic |
Hi @10gic, we're trying to investigate, how it can be fixed. Will let you know once I have an update |
I'm glad to hear this. Hope there is good news soon. |
Hi @vcoolish, do you have any idea why the memory consumption is still increasing in the test case above after the fix? |
Hi @10gic, could you please clarify which WalletCore package do you use? |
Hi @satoshiotomakan, I used |
could you try adding delay(1) to your while loop? |
I think adding a delay in the 'while' loop won't affect the test results. Could you please elaborate on your intention? |
GC doesn't clean the memory instantly and without 1 ms delay the test will throw outofmemory. Having 1ms delay simulates more real usage and allows GC to do the job |
Do you mean that the GC has no chance to clean up memory without a 1ms delay? After I finish the while loop, I think the GC has enough time to clean up the memory. However, I don't see the memory consumption drop after the while loop finishes. Anyway, I will test it with a 1ms delay in each iteration of the while loop. |
Hi @vcoolish, I have retested wallet-core-kotlin-android:4.2.0-dev-rc2 with a 1ms delay in each iteration. Here are the results: The native memory usage keeps increasing and has reached up to 200.1MB (almost the same as the previous result of 197.4MB). It seems that adding a delay doesn't work. |
ok, thank you for checking. Sergei has an idea what else can be done between C++ and rust we will investigate |
@10gic I debugged the test you provided above, and it turned out that the fix works fine in general. However For example, in this case var i = 1
while (i <= 200_000) {
val publicKey = PublicKey(pk, PublicKeyType.SECP256k1Extended)
val anyAddress = AnyAddress(publicKey, Ethereum, Derivation.Default)
i++
delay(1)
} However, if we move var i = 1
val publicKey = PublicKey(pk, PublicKeyType.SECP256k1Extended)
while (i <= 200_000) {
val anyAddress = AnyAddress(publicKey, Ethereum, Derivation.Default)
i++
// delay(1) // the memory clean up happening even without the delay
} |
@satoshiotomakan Thanks for your debugging. May I ask if there's a plan for when the dev branch can be merged into the master branch? |
Hi @10gic, we don't want to merge dev into master until we are sure memory leaks fixed. |
Describe the bug
I found a memory leak in AnyAddress within the Kotlin package of Wallet Core.
To Reproduce
The following code can trigger a memory leak:
Expected behavior
No memory leak.
Screenshots
Memory Profiler Screenshot:
Additional context
The above test was conducted on Android. Based on the generated code in directory wallet-core-kotlin, this issue should also exist on iOS.
The text was updated successfully, but these errors were encountered: