-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlin multiplatform leaking memory (#4037)
* Add deinit for KMP iOS and JVM targets * Add deinit for JS target * Add deinit for JS target * Fix JVM native name * Reuse one thread on JVM --------- Co-authored-by: satoshiotomakan <[email protected]>
- Loading branch information
1 parent
b4221b4
commit 00a8744
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...re-kotlin/src/commonAndroidJvmMain/kotlin/com/trustwallet/core/GenericPhantomReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.trustwallet.core | ||
|
||
import java.lang.ref.PhantomReference | ||
import java.lang.ref.ReferenceQueue | ||
|
||
internal class GenericPhantomReference private constructor( | ||
referent: Any, | ||
private val handle: Long, | ||
private val onDelete: (Long) -> Unit, | ||
) : PhantomReference<Any>(referent, queue) { | ||
|
||
companion object { | ||
private val references: MutableSet<GenericPhantomReference> = HashSet() | ||
private val queue: ReferenceQueue<Any> = ReferenceQueue() | ||
|
||
init { | ||
Thread { | ||
try { | ||
doDeletes() | ||
} catch (e: InterruptedException) { | ||
Thread.currentThread().interrupt() | ||
} | ||
}.apply { | ||
name = "WCFinalizingDaemon" | ||
isDaemon = true | ||
priority = Thread.NORM_PRIORITY | ||
start() | ||
} | ||
} | ||
|
||
fun register( | ||
referent: Any, | ||
handle: Long, | ||
onDelete: (Long) -> Unit, | ||
) { | ||
references.add(GenericPhantomReference(referent, handle, onDelete)) | ||
} | ||
|
||
private fun doDeletes() { | ||
while (true) { | ||
val ref = queue.remove() as GenericPhantomReference | ||
ref.onDelete(ref.handle) | ||
references.remove(ref) | ||
} | ||
} | ||
} | ||
} |