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
Addendum 1: Many people may have heard of this me...
Potatoes are a classic "side dish" that...
Screen: operate: Sound Effects: Plot: Experience:...
Sleep problems are plaguing the entire human race...
As the saying goes, "If you want to do your ...
March The earth warms up and everything recovers ...
Recently, the editor noticed that Apple has once ...
[[130939]] Apple's Apple Watch smartwatch wil...
The species calendar I wrote before was about the...
There is such a story: Once upon a time, a fierce...
1. Why do people like to pick their noses? Becaus...
Wall decoration is a very important step in the d...
Beijing time, March 18th, according to a report f...
Every year, many customers use Zhihu promotion pl...