WeakRef

interface WeakRef<out T>(source)

A weak reference holds a reference to another object, without preventing that object from being garbage-collected.

The value referenced by this class is eligible for garbage-collection, meaning that it could disappear at any time. Garbage collectors are complex machinery. One should take care not to write code that depends on the behavior of a specific garbage collector, as that behavior can change between future versions.

Storing null is technically allowed for convenience reasons, but doesn't make much sense. It is not possible to distinguish between a weak reference that stores null and one that used store a value but has been cleared. Therefore, implementations are free to not store null values at all.

Obtain instances

The default implementations are available via the top-level WeakRef and SoftRef functions. Other implementations are available in the algorithms subpackage.

Not stable for inheritance

This interface is not stable for inheritance. We may add new methods at any time.

Inheritors

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract fun read(): T?

Attempts to read the value held by this reference.