Weak Ref
A reference that does not keep its object alive, from common code.
The object that a WeakRef points to is its referent. While other code holds the referent, get returns it. After the garbage collector frees it, get returns null. Use it for caches and listeners that must not cause a memory leak.
Example
val ref = WeakRef(bitmap)
// get() returns null after the bitmap was collected.
val image = ref.get() ?: reloadBitmap()Content copied to clipboard
Platform differences
| Target | Weak? |
|---|---|
| JVM, Android, iOS, macOS | Yes, backed by the platform weak reference |
| JS, ES2021 and later | Yes, backed by the JS WeakRef, except for strings and numbers |
| JS before ES2021 | No, it holds the referent strongly |
| Wasm | No, it holds the referent strongly |
Watch out
Always handle the
nullfrom get. The collector can free the referent at any time after the last strong reference is gone.Where the referent is held strongly, only clear frees it. If your design needs the memory back, check isWeakSupported.
On JS, strings and boxed numbers are held strongly even when isWeakSupported is
true. Wrap them in your own class when that matters.
Parameters
referred
the referent.