Using the tool: Android Studio 2.0 Preview, Android Device Monitor, MAT (Memory Analyzer).
Click "Android Device Monitor" on the Android Studio toolbar, as
Open and select the application process, then click on "Update Heap", then click on each activity of the application repeatedly, the last "Dump HPROF file", as shown in 1-2-3
Save the Hprof file.
The hprof file needs to be converted below.
Open cmd terminal, go to \sdk\platform-tools directory, convert hprof file with hprof-conv command
Hprof-conv f:/a.hprof F:/b.hprof
Finally get the B.hprof file.
To official website: http://www.eclipse.org/mat/downloads.php Download the Memory Analyzer tool.
Choose the version that's right for you.
Open MemoryAnalyzer.exe, select "File", "Open Heap Dump" in the toolbar, select B.hprof
Select the action in the overview and click "Histogram"
You can search for class names in your project and support partial matches. After I enter radar, I match the following content
Right-click one of the, select "Merge shortest Paths to GC Roots", "Exclude all Phantom/weak/soft etc". References "
You can also use the Dominator tree in overview to generate a new list, again using path to GC Roots-exclude Weak/soft references to filter out classes that are associated with program-related memory leaks. Give an example:
Since Toastutil is a tool class, the method is static and accepts the context parameter, at which point the activity context is passed in an activity, Causes Toastutil to hold a reference to the activity and the activity is not released and memory leaks. Solution: Pass a application context in, let this context and toastutil have the same life cycle.
In addition, memory leaks can occur when handler is used improperly, such as using postdelayed in handler.
Common solutions include the following:
1. Using static internal handler/runnable + Weak reference
2. When ondestory, manually clear the message
3. Weakhandler of third parties developed with Badoo
For details, please refer to this article: using Android Studio to analyze memory leaks
How to analyze Android app memory leaks with Mat