Project performance optimization-memory leak detection and modification

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

Insert image description here

Insert image description here

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.
Insert image description here

Step 3. Select "Capture heap dump" and click "Record"

Insert image description here

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.
Insert image description here
You can see that 2 places that will cause memory leaks are captured. Click to view detailed memory leaks
Insert image description here
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.
Insert image description here
Insert image description here
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.
Insert image description here

Step 6: Retest

Insert image description here
At this point, the risk of memory leaks has been fixed.

Guess you like

Origin blog.csdn.net/u011557841/article/details/131481725
Recommended