//leakcanary/leakcanary/ObjectWatcher
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¶
ObjectWatcher | fun ObjectWatcher(clock: Clock, checkRetainedExecutor: Executor, isEnabled: () -> Boolean = { true }) |
Functions¶
Name | Summary |
---|---|
addOnObjectRetainedListener | @Synchronized fun addOnObjectRetainedListener(listener: OnObjectRetainedListener) |
clearObjectsWatchedBefore | @Synchronized fun clearObjectsWatchedBefore(heapDumpUptimeMillis: Long) Clears all KeyedWeakReference that were created before heapDumpUptimeMillis (based on clock) |
clearWatchedObjects | @Synchronized fun clearWatchedObjects() Clears all KeyedWeakReference |
expectWeaklyReachable | @Synchronized open override fun expectWeaklyReachable(watchedObject: Any, description: String) Expects the provided watchedObject to become weakly reachable soon. If not, watchedObject will be considered retained. |
removeOnObjectRetainedListener | @Synchronized fun removeOnObjectRetainedListener(listener: OnObjectRetainedListener) |
Properties¶
Name | Summary |
---|---|
hasRetainedObjects | @get:Synchronized 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. |
hasWatchedObjects | @get:Synchronized 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. |
retainedObjectCount | @get:Synchronized 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. |
retainedObjects | @get:Synchronized 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. |