ObjectWatcher

class ObjectWatcher(    clock: Clock,     checkRetainedExecutor: Executor,     isEnabled: () -> Boolean = { true }) : ReachabilityWatcher

ObjectWatcher can be passed objects to watch. It will create KeyedWeakReference instances that reference watches objects, and check if those references have been cleared as expected on the checkRetainedExecutor executor. If not, these objects are considered retained and ObjectWatcher will then notify registered OnObjectRetainedListeners on that executor thread.

checkRetainedExecutor is expected to run its tasks on a background thread, with a significant delay to give the GC the opportunity to identify weakly reachable objects.

ObjectWatcher is thread safe.

Constructors

Link copied to clipboard
fun ObjectWatcher(    clock: Clock,     checkRetainedExecutor: Executor,     isEnabled: () -> Boolean = { true })

Functions

Link copied to clipboard
fun addOnObjectRetainedListener(listener: OnObjectRetainedListener)
Link copied to clipboard
fun clearObjectsWatchedBefore(heapDumpUptimeMillis: Long)

Clears all KeyedWeakReference that were created before heapDumpUptimeMillis (based on clock)

Link copied to clipboard
fun clearWatchedObjects()
Link copied to clipboard
open override fun expectWeaklyReachable(watchedObject: Any, description: String)

Expects the provided watchedObject to become weakly reachable soon. If not, watchedObject will be considered retained.

Link copied to clipboard
fun removeOnObjectRetainedListener(listener: OnObjectRetainedListener)

Properties

Link copied to clipboard
val hasRetainedObjects: Boolean

Returns true if there are watched objects that aren't weakly reachable, and have been watched for long enough to be considered retained.

Link copied to clipboard
val hasWatchedObjects: Boolean

Returns true if there are watched objects that aren't weakly reachable, even if they haven't been watched for long enough to be considered retained.

Link copied to clipboard
val retainedObjectCount: Int

Returns the number of retained objects, ie the number of watched objects that aren't weakly reachable, and have been watched for long enough to be considered retained.

Link copied to clipboard
val retainedObjects: List<Any>

Returns the objects that are currently considered retained. Useful for logging purposes. Be careful with those objects and release them ASAP as you may creating longer lived leaks then the one that are already there.