Leakcanary源码阅读

2.7版本已经不再需要手动初始化leakcanary

在编译打包后

<provider
    android:name="leakcanary.internal.AppWatcherInstaller$MainProcess"
    android:enabled="@ref/0x7f040007"
    android:exported="false"
    android:authorities="com.cjj.testdemo.leakcanary-installer"/>
override fun onCreate(): Boolean{
val application =context!!.applicationContextas Application
  **AppWatcher.manualInstall(application)**
return true
}
@JvmOverloads
fun manualInstall(
application: Application,
  retainedDelayMillis: Long = TimeUnit.SECONDS.toMillis(5),
  **watchersToInstall: List<InstallableWatcher>= appDefaultWatchers(application)**
) {
....
<p>watchersToInstall.forEach{
<strong>it.install()</strong>
}
}
fun appDefaultWatchers(
application: Application,
reachabilityWatcher: ReachabilityWatcher = objectWatcher
): List<InstallableWatcher> {
returnlistOf(
<strong>ActivityWatcher(application, reachabilityWatcher)</strong>,
FragmentAndViewModelWatcher(application, reachabilityWatcher),
RootViewWatcher(reachabilityWatcher),
ServiceWatcher(reachabilityWatcher)
)
}
class ActivityWatcher(
private val application: Application,
private val reachabilityWatcher: ReachabilityWatcher
): InstallableWatcher{</p>
<p>private val lifecycleCallbacks =
object : Application.ActivityLifecycleCallbacks bynoOpDelegate() {
override fun <strong>onActivityDestroyed(activity: Activity)</strong> {
<strong>reachabilityWatcher.expectWeaklyReachable(</strong>
activity, "${activity::class.java.name} received Activity#onDestroy() callback"
)
}
}</p>
<p>override fun install() {
<strong>application.registerActivityLifecycleCallbacks(lifecycleCallbacks)</strong>
}</p>
<p>override fun uninstall() {
application.unregisterActivityLifecycleCallbacks(lifecycleCallbacks)
}
}</p>
<p>
@Synchronized override fun expectWeaklyReachable(
watchedObject: Any,
description: String
) {
if(!isEnabled()) {
return
}
removeWeaklyReachableObjects()
val key = UUID.randomUUID()
.toString()
val watchUptimeMillis = clock.uptimeMillis()
<strong>val reference =
KeyedWeakReference(watchedObject, key, description, watchUptimeMillis, queue)</strong>
SharkLog.d{
"Watching " +
(if(watchedObject is Class<*>)watchedObject.toString()else "instance of${watchedObject.javaClass.name}")+
(if(description.isNotEmpty())" ($description)" else "")+
" with key $key"
}</p>
<p><strong>watchedObjects[key]= reference
checkRetainedExecutor.execute{
moveToRetained(key)</strong>
}
}
@Synchronized private fun moveToRetained(key: String) {
<strong>removeWeaklyReachableObjects()</strong>
val retainedRef = watchedObjects[key]
if(retainedRef != null) {
retainedRef.retainedUptimeMillis = clock.uptimeMillis()
onObjectRetainedListeners.forEach{ <strong>it.onObjectRetained()</strong>}  //这个方法里面调用了GC
}
}
private fun removeWeaklyReachableObjects() {
// WeakReferences are enqueued as soon as the object to which they point to becomes weakly
// reachable. This is before finalization or garbage collection has actually happened.
var ref: KeyedWeakReference?
do{
ref = queue.poll()as KeyedWeakReference?
if(ref != null) {
watchedObjects.remove(ref.key)
}
}while(ref != null)
}

流程就很明显了,leakcanary在Activity销毁时,持有一个弱引用,循环移除弱引用中已经销毁的对象的key,在gc后不断检查弱引用的对象是否存在,存在则调用gc在查一遍,如果还存在,就dump出来