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
- First click on the Profile
- Click the area in Memory
- Click on a moment you want to detect
- Click Dump Java Heap
At this point, a memory leak has been found
- 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)
- Double-click SecondActivity to refer to the memory used by the object
- 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.