WhileUsed

constructor(retentionMillis: Long = 0, destructor: (T) -> Unit? = null, builder: (WhileUsedReferenceToken) -> T)

Parameters

retentionMillis

Defines a retention period in milliseconds in which to still keep the value in RAM although the reference count returned to 0. If the value is requested again within this retention period, the old value is reused. Defaults to 0 (immediately frees the value).

destructor

Optional destructor which can clean up the object before it gets freed. Defaults to null in which case, if the value is a Disposable, its dispose() method is called. Pass an empty lambda function if you don't want this behavior.

builder

Should create and return the value. The builder gets a DisposableGroup as its argument for retrieving other WhileUsed values or for adding other Disposables which must be cleaned up together with this value (as an alternative to using destructor).