1. LeakCanary is introduced
LeakCanary is Square’s open source memory leak detection tool, which can help us with our daily development. If you have seen JVM memory reclamation, you should know the cause of the leak. Today I mainly from the source code to see his implementation.
2. Source code analysis
LeakCanary we have started with the install method, which is executed in the Application#onCreate method when we introduce LeakCanary.
The isInAnalyzerProcess method is not allowed to initialize other tasks if it is the LeakCanary process. This process is used for LeakCanary to analyze heap memory.
We know that importing LeakCanary automatically installs a Leaks APk once your project is successfully compiled. LeakCanary. Install (this) : Is Leaks testing my project across processes? What is the detection principle? With these questions in mind, let’s look at what we did. The LeakCanary# Install method is called
First, what exactly does the buildAndInstall method do
ActivityRefWatcher class name should be Activity reference monitor, let’s follow up with the installOnIcsPlus method
The installOnIcsPlus method then calls the ActivityRefWatcher#watchActivities method, and we follow up
We first look at the application # registerActivityLifecycleCallbacks method, we know registration principle registerActivityLifecycleCallbacks is the Activity lifecycle callback methods. Methods to manage the lifecycle, to which every Activity is called back. Let’s look at the lifecycleCallbacks variable that corresponds to the Activity lifecycle. LeakCanary is actually monitored for memory leaks by registering an Activity lifecycle callback. Call the corresponding onActivityDestroyed method when the Activity screen is closed. Next we see Application. ActivityLifecycleCallbacks# onActivityDestroyed method did something.
Will call to the next com. Squareup. Leakcanary. RefWatcher# watch (java.lang.object) method.
Wrap its Activity reference as a KeyedWeakReference weak reference. The reference wrapped by WeakReference will be added to the ReferenceQueue queue if reclaimed. WeakReference and ReferenceQueue will be analyzed in the following article. WeakReference and ReferenceQueue detect whether the Activity reference in the ReferenceQueue can be reclaimed by checking the Activity reference in the ReferenceQueue. Here are the specific detection methods:
The method name ensureGone means to ensure that references are freed. Look specifically at what the ensureGone method does:
We basically see removeWeaklyReachableReferences (), gcTrigger runGc (), heapdumpListener. Analyze these three methods ().
1. By removeWeaklyReachableReferences removes all ReferenceQueue queue WeakReference object reference Activity. If you can recycle it, return it.
2. The Activity references if through removeWeaklyReachableReferences or passed gcTrigger without removing runGc () to the GC trigger collection.
3. Then trigger a step 1 removeWeaklyReachableReferences method to remove the Activity reference, determine whether recycling, The heapdumpListener. Analyze () method is used to analyze the HeapDump object. We can dump the current Heap hprof snapshot file using the heapDumper.dumpheap () method. Concrete through the Debug. DumpHprofData (heapDumpFile getAbsolutePath () method to dump the memory file. The implementation is as follows:
Then call com. Squareup. Leakcanary. ServiceHeapDumpListener# analyze method to memory file directory and other related information by HeapAnalyzerService# runAnalysis approach to object of memory leaks Analysis. The implementation is as follows:
HeapAnalyzerService IntentService is then started.
In the HeapAnalyzerService#onHandleIntent method, HeapAnalyzer#checkForLeak is used to detect whether the memory leaks. The implementation is as follows:
After the next start AbstractAnalysisResultService the IntentService, start will call onHandleIntent method.
Finally, onHeapAnalyzed is used to pass heapDump and AnalysisResult information into the subclass implementation.
Analyzing the above logic is to display memory leaks through DisplayLeakService.
3. Summary
LeakCanary use application # registerActivityLifecycleCallbacks The Activity lifecycle callback onActivityDestroyed method checks whether the object is recycled by calling RefWatcher#wather. Through removeWeaklyReachableReferences – > gcTrigger. RunGc – > removeWeaklyReachableReferences – > heapdumpListener. Analyze Three-step two-step detection is performed to determine memory leaks. Finally, memory information is dumped and analyzed to display the leaked information.