Recently I finally had time to optimize the performance of a wave of projects. The first wave used the Profiler tool that comes with Android Studio to detect memory leaks.
The first step is to create Profiler's SESSIONS
Step 2: Enter MEMORY memory monitoring
The green origin on the right is the SESSION of the Profiler running at this time. Click MEMORY on the right to enter the details module of memory monitoring.
Step 3. Select "Capture heap dump" and click "Record"
Step 4. View the Heap Dump file
After crawling for a period of time in the third step, it will automatically stop and open the Heap Dump file.
You can see that 2 places that will cause memory leaks are captured. Click to view detailed memory leaks
2: The category where the memory leak is located
3: Memory Leak list
4: Check the reference chain of the memory leak
5: Show the nearest GC point 6
: Show the detailed location and variables that will cause the memory leak.
Use the List collection in the main Fragment to maintain the sub-Fragment to be switched, so that the Fragment object is associated with the collection , the object cannot be destroyed in time, and there will be a risk of memory leaks.
Step 5: Modify the memory leak point
Directly hand over the life cycle of Fragment to FragmentManager and find Fragment by setting TAG.
Step 6: Retest
At this point, the risk of memory leaks has been fixed.