Preface Through the memory leak detection and improvement of several applications in the past few days, the effect is obvious:
Judging from the results, my method of analyzing and improving memory leaks was correct. The process is not complicated, so I can summarize it for sharing. in principle For performance issues, analysis and improvement must follow the following principles:
step Here are the steps I took to solve the memory leak performance problem: Prioritize common memory leak issues First, solve the common memory leak problem. In this process, you can use Android Studio's Analyze-Inspect Code to perform static analysis on the code. Common memory leak problems include:
applicationUse scenarios Note: Please note that some numbers are added to some NOs. In fact, these are YES in terms of ability, but why are they NOs? The following explains them one by one: 1. Number 1: It is possible to start an Activity in these classes, but you need to create a new task, which is generally not recommended; 2. Number 2: It is legal to inflate layout in these classes, but the system default theme style will be used. If you customize some styles, they may not be used; 3. Number 3: Allowed when Receiver is null, used to get the current value of sticky broadcast in version 4.2 or above. (Can be ignored); 4. ContentProvider and BroadcastReceiver are in the above table because they have a context for use in their internal methods. There is another type that does not belong to memory leaks, but should be solved when analyzing memory leaks: for the same APP, if you put pictures in different drawable folders, the memory occupied on the same device will be different. For details, please refer to: Research and Analysis on the Relationship between Picture Size, Memory Occupancy and Drawable Folders in Android. To solve this problem, just follow the following principles: 1. The UI only provides a set of high-resolution pictures. It is recommended to put the pictures in the drawable-xxhdpi folder (it is not necessary to put them in the xxxhdpi or higher resolution folders. Weigh the pros and cons and take care of mainstream devices). In this way, the size of the pictures is only compressed on low-resolution devices, and there will be no increase in memory; 2. For desktop plug-ins or pictures that do not need to be scaled, put them in the drawable-nodpi folder. The pictures in this folder will not be scaled on any device. Use tools to check memory leaks after the program is running Through the above steps, most memory leak problems in the application can be solved. For some memory leaks, you need to run the program and analyze the memory snapshot after running to solve them, such as not unregistering after registration, memory leaks caused by static member variables in the class, memory leaks in the SDK, etc. Solving such problems can be done in two steps:
Note: In Android Studio, you can obtain the memory information of the currently selected process in the following ways:
Verify the improvement effect Based on my personal experience, I usually verify the improvement effect in this way: run the program, run each function once, make sure there are no problems with the changes, exit the program completely, manually trigger GC, and then use adb shell dumpsys meminfo packagename -d to check whether the number of Activivites and Views is close to 0; if it is not 0, use Leakcanary to check for possible memory leaks, continue to use MAT analysis, and repeat this cycle until you are satisfied with the improvement. Recommended Reading
|
<<: Android immersive status bar implementation
>>: Detailed explanation of Android Bitmap cache pool usage
Now many friends have opened accounts on Douyin. ...
Hangzhou Changjiang Automobile Co., Ltd. was form...
Shi Sanxi's "Controlling the Four Divine...
It turns out that this is the most fiery volcano ...
Tik Tok is a short video software used by many yo...
When it comes to copywriting , the first thing pe...
We all know that the former chairman of Coca-Cola...
It can be said that operation is the soul of a st...
Conversion is the key factor in paid promotion . ...
Earlier this year, there were reports that Apple ...
Reviewer of this article: Zhou Xiaobo, Doctor of ...
[[338502]] In 2016, I realized something very imp...
A recent study of more than 300 patients with chr...
Why do Tik Tok ? How to make Tik Tok? For enterpr...
In the circle, whenever the word "growth&quo...