How do I test the memory usage of an app? What portion of memory does an app occupy? How do I check if an app has a memory leak?
First, the introduction of Android Memory:
In the Java development process, the object is allocated memory through new, and the release of the memory is collected by the garbage collector (GC), in the development process, do not need to explicitly manage the memory, Java Virtual opportunity automatically help us reclaim memory. However, it is possible to unknowingly waste a lot of memory, resulting in the Java Virtual machine to spend a lot of time to garbage collection, more serious is caused by the JVM Oom.
Second, the app occupies the memory of which
The memory in the Android system, like the Linux system, has a lot of shared memory. Each app accounts for two private and public parts: Sharedirty, Privatedirty. PSS is a kernel computing scale that considers shared memory-basically, each memory page of a process is scaled down by a ratio that is related to the number of other processes that also use the page. In theory, you can accumulate the PSS footprint for all processes to check the memory footprint of all processes, or you can compare the PSS of the process to roughly discover the individual weights of the process. Privatedirty, it is basically a process that cannot be paged to disk memory, nor shared with other processes.
In the system settings in the phone, you can view the memory of the running application, and the memory shown here is the total Pss occupied by the process. So we just need to look at the value of total PSS to know the size of the memory the app is running on.
Third, how to view the memory occupied by an app
There are roughly three ways to view memory:
1. View by System Setup
Running->app in system settings, applications
Advantages: Simple operation
Cons: Inaccurate values, no real-time view of numeric changes
2. View from the command line
adb shell Dumpsys meminfo yourpakagename
The result is as shown
Where PSS corresponds to the total value of the memory is actually occupied by the value
Advantages: Simple and convenient, comprehensive and accurate data
Cons: Unable to view memory consumption in real time
3. Viewing through the system API
First get the list of running programs by Activitymanager and find the PID of the program you want to get.
Activitymanager.getrunningappprocesses ()
Then through MEMORYINFO[0].GETTOTALPSS (); method to get actual memory consumption
Gettotalprivatedirty and Gettotalshareddirty methods are also included in the Memoryinfo
Advantages: High scalability, can be viewed in real-time memory consumption;
Disadvantage: need to have development ability, start more difficult
So now that we're testing memory, we're using an app that's built in-house to monitor the memory, and the app is now able to monitor and record data results in real time, providing data support for developers and testers to analyze memory. It still belongs to the beta stage and has the opportunity to be available to you later.
Iv. memory leaks
What is a memory leak? Memory leaks, also known as "storage leaks," are dynamically opened up with dynamic storage allocation functions and are not released after use, resulting in the memory unit being occupied until the end of the program.
Examples of memory leaks:
Btn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
for (int i = 0; i <; i++) {
ImageView img = new ImageView (mainactivity.this);
Img.setimageresource (R.drawable.ic_launcher);
Test.add (IMG);
}
}
});
Where test is a static list<imageview>.
This way, if you keep clicking Btn, a memory leak will occur.
How do we monitor memory leaks?
Cases:
To test the activity with an example of a memory leak above, when the button is clicked, a picture is added to a static array, creating a memory leak scenario.
Enter the test activity to record the memory occupied by the current app before clicking the button, then click the button. Wait for the operation to complete, make a GC, and then look at the memory occupied by the app.
The memory used by the app after it returns does not come down significantly, indicating that a memory leak can occur in the code.
That is, once a GC is performed after performing some operation, the memory does not fall significantly. At this point, you can conclude that there may be a memory leak in your code.
Detection method:
- Use the three ways above to see how memory is used
- Using the heap in DDMS:
1) Open Ddms and open the Devices view and the heap view
2) Click to select the process to monitor
3) Check the "Update heap" icon on the Devices view interface
4) Click the "Cause GC" button in the heap view (equivalent to a GC operation)
In general, we will observe the total value of the data object, normally after each GC, this value will have a significant fall down and will be stable in a range, stating that there is no memory in the code is not released, if this value does not appear after each GC a significant decline, There may be memory in the code that has not been freed.
General: Memory is not only a performance test need to pay attention to, as a good development engineer should pay attention to their own code memory usage, so as to avoid the situation of oom. To know that the phone is allocated to each process of memory is not many, when the system memory is not enough to kill some of the high memory process, so in order not to be killed by the system we should use memory as reasonable as possible to avoid memory leaks.
On the memory of Android application performance (RPM)