WeakRef
A weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collectedA weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collectedA weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collectedA weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collectedA weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collectedA weak reference to T. Holds its referent without preventing garbage collection.
get returns the referent, or null once it has been collected or after clear. Query isWeakSupported before relying on collection for correctness.
Platform behavior:
JVM, Android, Apple/Native: backed by the platform weak reference.
Kotlin/JS: backed by the ES2021
WeakRefglobal. On a runtime older than ES2021, and for referents backed by JS primitives (String, boxed numbers) that are not validWeakReftargets, the referent is held strongly.Kotlin/Wasm: no weak primitive exists, so the referent is held strongly and isWeakSupported is
false.
val weak = WeakRef(expensiveThing)
val thing = weak.get() ?: rebuild() // null if it was collected