First, I wrote a memory leak caused by the singleton pattern

public class Manager { public static final Manager ourInstance = new Manager(); private Context mContext; public static Manager getInstance() { return ourInstance; } public void init(Context context) { mContext = context; }}Copy the code

Then create a new SecondActivity that calls the Init method of the Manager, passing the SecondActivity context to the Manager. When the SecondActivity is destroyed, it cannot be recycled, causing a memory leak.

PS: Huawei mobile phone can not be analyzed by Profile, you can use Xiaomi or OpPO mobile phone

Click on the profile

  1. First click on the Profile
  2. Click the area in Memory

  1. Click on a moment you want to detect
  2. Click Dump Java Heap

At this point, a memory leak has been found

  1. Click Arrange by Package, you can find that SecondActivity has not been recycled in the directory of package (at this point, I have already quit the page)
  2. Double-click SecondActivity to refer to the memory used by the object
  3. Click References to see the chain of References to this object, and then you can locate the specific problem to solve the memory leak

Give yourself a thumbs up for analyzing memory leaks with profiles.