美文网首页
Leakcanary总结

Leakcanary总结

作者: 曾大稳丶 | 来源:发表于2022-01-13 14:47 被阅读0次

    2.7版本监控对象:
    activity
    fragment
    viewmodel(自己创建一个viewmodle,在clear的时候hook获取ViewModelStore里面的mMap,就可以获取viewmodel进行检测。
    view(hook windowmanagerglobal的mviews,替换为自己的arraylist,在add函数在addOnAttachStateChangeListener,触发onViewDetachedFromWindow的时候检测。
    sevice(hook activitythread的mcallback)

     fun appDefaultWatchers(
        application: Application,
        reachabilityWatcher: ReachabilityWatcher = objectWatcher
      ): List<InstallableWatcher> {
        return listOf(
          ActivityWatcher(application, reachabilityWatcher),
          FragmentAndViewModelWatcher(application, reachabilityWatcher),
          RootViewWatcher(reachabilityWatcher),
          ServiceWatcher(reachabilityWatcher)
        )
      }
    
    

    触发gc

    override fun runGc() {
          // Code taken from AOSP FinalizationTest:
          // https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/
          // java/lang/ref/FinalizationTester.java
          // System.gc() does not garbage collect every time. Runtime.gc() is
          // more likely to perform a gc.
          Runtime.getRuntime().gc()
          enqueueReferences()
          System.runFinalization()
    }
    
    

    dump

    Debug.dumpHprofData(heapDumpFile.absolutePath)
    

    Analysis,采用shake

    heapAnalyzer.analyze()
    

    相关文章

      网友评论

          本文标题:Leakcanary总结

          本文链接:https://www.haomeiwen.com/subject/qrvwcrtx.html