Suspected Activity memory leak, how to locate the leak?

  1. Step 1: Verify that a leak has occurred

Use Android Studio to open the Memory profiler [View->Tool Windows], select your own process, select Memory, enter the Activity you want to check, and then exit the Activity, click GC several times. Let the Activity be recycled as much as possible

Wait a few seconds and click the stack capture button

View the captured stack information in Android Studio. In the filter box, enter your Activity and see if it is in the list. If not, congratulations, your Activity has not been leaked, rest assured online! If so, congratulations, you can learn another move, haha

  1. Step 2: Use MAT to locate the memory leak

    2.1 download the MAT

    Address: https://www.eclipse.org/mat/Copy the code

2.2 installation MAT

If you're on a MAC and can't open it after installation, don't panic, go to the app, go to Contents/MacOS/, double-click MemoryAnalyzer and you'll be OK, and the MAT will start up. Do the same if you want to open it in the futureCopy the code

2.3 save the stack log just captured in AndroidStudio

2.4 What is saved is a.hprof file. This.hprof file cannot be recognized by MAT, and it needs to be converted using Hprof-conv under Android SDK. This tool is stored in SDK/platform-tools, and conversion is very convenient. Run the Terminal command line in Android

Hprof-conv-z original file [path] new file [path]Copy the code

2.5 Use MAT to open your converted file

2.6 After opening, the page is displayed as follows: Click Histogram:

2.7 Enter your Activity name in the Histogram filter and your Activity instance usage in memory will appear. The Objects column represents the format of the Activity instance in memory:

Step 3: Find Activity memory leaks: Right click on your Activity and select Path GC Roots.

Ok, the memory leak has been found

This above introduction only involves steps, specific working principle, I hope to see colleagues, their own research, here will not do a detailed description, about some electricity, layout optimization or investigation can also contact me, I wish the work is smooth