The installation of LeakCanary

LeakCanaryProvides an example calledLeakSentryInstallerContentProvider

The installation of ContentProvider

To recap, Zygote calls the ActivityThread.main() method when it receives an AMS request to create an APP process. There are three main things Zygote does:

  1. createActivityThread()object
  2. createApplicationThread()object

    final ApplicationThread mAppThread = new ApplicationThread();
  3. callActivityThread.attach;

ActivityThread. Attach after a series of calls to ApplicationThread. BindApplication method, then sends a sendMessage (H.B IND_APPLICATION, data); When received, in the handleBindApplication method, Call installContentProviders respectively (install ContentProvider) and mInstrumentation callApplicationOnCreate (app); (app onCreate method), until the app has been started. The diagram below:







InstallContentProviders invokes the process







LeakCanary principle

LeakCanary

LeakCanary detection is achieved by registerActivityLifecycleCallbacks of activity

Code that initiates memory leak detection

RefWatcher core principles

RefWatcher maintains two collections. After the watch call, the weak references of the object are first added to the collection watchedReferences. After a delay of five seconds, the object is obtained from the queue. At this point, move the reference object into retainedReferences and temporarily mark it as leaking



LeakCanary Detects the principle of memory leak

The core processes

Calling process