Ram Evaluation Optimization Scheme

Source: Internet
Author: User
Tags sorts

1. Purpose and significance

This is the optimization of the evaluation method.

The importance of RAM as the place where all content is loaded when the hand is running is self-evident.

Knowing the amount of RAM left on the phone after it is turned on will help us understand how much of the program the phone can load and the ability to carry the app is high.

Android is a constantly evolving system, the new version of the need for RAM more and more, understand the current system in normal use, the system consumes RAM size, also can help development engineers, development managers from the bottom to clear the state of the system itself. Provide a very necessary basis for development or decision-making.

2. Terminology

RAM (random access memory). Is the memory that is usually said.
Memory consumption noun parsing:

Terminology full Spell explain
Vss Virtual Set Size Virtual memory consumption (contains memory consumed by shared libraries)
Rss Resident Set Size Actual use of physical memory (contains memory consumed by shared libraries)
Pss Proportional Set Size Physical memory actually used (proportional allocation of memory consumed by shared libraries)
Uss Unique Set Size The physical memory occupied by the process alone (does not contain the memory occupied by the shared library)

In general, memory footprint has the following rules: VSS >= RSS >= PSS >= USS.

3. Tools

Observing and calculating the memory usage of the system, Android mainly provides us with two tools procstats,meminfo. The former focuses on memory usage in the background, which is memory usage at runtime. There are other configuration files, and Linux commands can also get some helpful parameter information.

3.1 Process Stats:

Procstats is a new system service proposed by Android 4.4 KitKat. It can help us to better understand the memory usage of the app in the background (background).

Procstats can monitor the app's behavior over time, including how long it has been running in the background, and how much memory has been used during this period. This helps you quickly find inefficient and irregular areas of your application to avoid affecting their performs, especially when running on low-memory devices.

We can use the procstats (adb shell Dumpsys procstats–hours 3) via the ADB shell command, or it is more convenient to run the process Stats Developer Tools (Click Settings > Developer options > Process Stats in the 4.4 version of the phone) and click on a single entry to see more details.

3.2 Meminfo:

Android also provides a tool called Meminfo. It calculates the memory usage of each process according to the PSS standard and sorts it by importance.

We can do it via the command line: (adb shell Dumpsys meminfo) or use the device to click Settings > Apps > Running (unlike Procstats, it can also run on older versions).

3.3 Procrank: (Root required)

It calculates the memory usage of each process and sorts it by importance.
We can execute it by command: (adb shell Procrank).

3.4 Reading/proc/meminfo Information

The meminfo here is not the same as in 3.2, where only the approximate usage of memory is obtained.
We can execute it by command: (adb shell cat/proc/meminfo).

3.5 PS:

It displays all process information for the current Android system, including the RSS size of the process.
We can execute it by command: (ADB shell PS).

3.6 Viewing individual process memory usage:

Method One: PS process pid
Method Two: Showmap process pid
Method Three: Dumpsys meminfo process pid or process name

4. Memory Analysis

A mobile phone to be able to run normally, from the software level is broadly divided into the following parts:

Modem: the software to be used for mobile communication.
Kernel: The core of the system, the specific version of the Linux kernel library used by Android, provides device drivers, and so on.
System service : Systems Services, which is the system runtime, that can be used together by the upper-level framework.
Frameworks: application framework, supplied by Android with standard APIs, supplied with run and call.
Applications: applications, which are user-accessible software that can actually be used to provide a variety of phone functions.

From the memory level, all of the above will be loaded into memory after the system is powered on. Therefore, we want to understand how much memory space is consumed by these parts after the system is booted properly.

However, the Android operating mechanism requires another piece of memory that is not mentioned, which is frame Buffer. In addition, we think that the unused space of RAM is not used. The remaining space of RAM measures the ability of the system to continue to load the program and run normally, although the remaining space and running speed are not positively correlated, but from the program stability, the larger the remaining space is not easy to lead to memory recycling, the program is forced to clean up the system caused by lag, instability, slow reaction is lower.

Frame Buffer: The space to use for the system to run (not software, just a piece of memory space).
**free:**ram the remaining space.

In conclusion, the goal of RAM evaluation is to get more realistic and accurate data sizes in the table below.

Free
Product Perso Modem Frame Buffer Kernel System Service Frameworks Applications
5. Evaluation method 5.1 Get fixed value (Modem, Frame Buffer)

A project, its modem occupies the size, Frame buffer is pre-set by the system engineer size, set to how big, will be fixed occupy how much memory (even if the actual modem does not reach the set size, because this space has been divided into the modem use, Also cannot be used temporarily by the system, if the actual value and setting value is too large, it is a place that can be optimized.

In the MTK platform, these two values can be read in a configuration file called Mtk_memcfg. Use the following command (the phone requires root):

adb shell cat /proc/ mtk_memcfg/memory_layout

The resulting return results are as follows:

By reading this file, we can get a lot of configuration information about memory.

We focus on the following information, which I explain while explaining.

available DRAM size: that is the amount of memory.
FB: is the size of frame buffer.
Mtk_wcn_consys_memory_reserve: the size of the WiFi required.
Ccci_md_mem_reserve: It is the size required for modem.
kernel: The required size of the kernel, but not accurate, we will find that there are two lines, and the size is not the same. So, the true size of kernel, we need to get it by other means. Special attention is needed here.

In the example, this step we can get 4 specific values, which are counted in the following table:

Data Labels hexadecimal size (Byte) decimal Size (MB)
Total Available DRAM size 0x20000000
Fb Fb 0xb00000
Modem Ccci_md_mem_reserve 0x01800000

It is necessary to note that the last hex in the hexadecimal representation is the memory size, in bytes. There are two values separated by a short connector before this value, indicating the starting and closing bits of the memory address space. We converted the 16 into decimal and then converted the units into megabytes to get the final desired data.

5.2 Get Kernel value

Next get the size of the kernel.

Here is another place to read the memory information, using the following command:

adb shell cat /proc/meminfo

The returned results are as follows:

We focus on the following information, and I continue to explain it as a side note:

memtotal: The total amount of memory available to the system and the user (it is smaller than the actual physical memory, because there is some memory to use for RADIO,DMA buffers, etc.).
Memfree: The remaining available memory size. Generally the value of Android system is very small, because we try to keep the process running, which consumes a lot of memory.
Cached: This is the memory used by the system for file buffering and so on.

Here, we are mainly to get the size of the memtotal, the conversion is 449MB.

In this way, we can calculate the size of the kernel using the following formula:

= Total-FB-Modem-MemTotal

By the data obtained previously, it can be calculated that the size of the kernel is 27MB.

5.3 Get statistical values (System Service, frameworks, applications)

Due to the Android system, different processes are started in the background during the non-specific period, which results in the fact that we use the Meminfo tool for real-time statistics is not very accurate. For example, using a method that waits 5 minutes for monitoring after power-on, each operation does not get a particularly stable value, and sometimes even up to dozens of trillion of differences.

Therefore, we use the historical statistic value of Procstats, using the following command:

shell--hours 1

Count your phone's memory usage within 1 hours of normal standby.

The results returned are as follows:

The above does not show exactly.

With this command, we can get all the processes and corresponding information that are loaded into memory within the last 1 hours of the system.

Let me focus on how to understand the above content:

percent: represents the total amount of time that the process consumes in various states. For example, 100% means that the process has been in operation during this time period.
Total : represents the overall occupancy of the process.
**imp FG:**FG is the abbreviation of foreground. This I guess is meant to be loaded into the foreground.
Service : identifies whether the services are.
persistent: identifies whether it resides in memory all the time and, like a service, represents a level of memory emplacement.
Top: identifies whether it is a top-level process.
Receiver: identifies whether it is a broadcast process.
(Cached): identifies whether it is a cache. The point is that because Android inherits the idea of Linux, it thinks that memory doesn't need to be white, so it caches a certain amount of content into memory so that it can be read quickly when it needs to be used. As a result, some of the process content that may not be currently required is also loaded into the content, and if memory is tight it will be released. According to this principle, we treat the cached space as free.
data groups in megabytes: represents the size of memory values that are occupied, arranged in the following order:
Minuss-avguss-maxuss/minpss-avgpss-maxpss
Over number: refers to the calculation of the memory value of the total number of samples taken, which is taken by the system itself to sample.

Our goal is to filter out the memory footprint that belongs to system Service, frameworks, applications from these data.

Here's how:

We use the Excel tool to sort out the resulting data, filtering out the process where total corresponds to a non-empty memory value.

These processes are then categorized according to system Service, frameworks, applications three. Specifically which process belongs to which, need according to personal experience to judge, applications is relatively easy to distinguish, System service and frameworks may have difficult to distinguish situation.

We provide a reference classification, which basically includes the processes that are often encountered, which can be used to ensure the accuracy and uniqueness of the data. The Reference classification table is as follows:

System Service:
System
Non-third-party application process with suffix service
Framework:
Com.android.systemui
Com.android.phone
Android.process.media
Zygote
MediaServer
Drmserver
Surfaceflinger
ServiceManager
APP:
< com.android.inputmethod.latin>
< com.android.keyguard>
< com.android.launcher3>
< com.android.nfc>
< com.dolby>
< com.mediatek.bluetooth>
< com.mediatek.hotknot.service>
< com.mediatek.mtklogger>
< com.mediatek.voicecommand>
< com.mediatek.voiceextension>
......

For each memory value, we select Avgpss, which is the second-to-last value of the data group in megabytes, and the value of system service and applications can be obtained according to the above classification.

For the value of frameworks, we need to do some special processing, because we can only get Com.android.systemui, Com.android.phone, procstats from the general Android.process.media the size of several processes, and while the framework is running in the system, more than just these processes, we also add an extra few, based on experience, such as Zygote, MediaServer, Drmserver, The size of the Surfaceflinger, ServiceManager at run time.

How are the sizes of these extra processes available? The answer is obtained from Meminfo, because Meminfo gets the real-time size, and for the system, the general framework of the process changes will not be very large. However, we can not directly use adb shell Dumpsys meminfo to obtain, because from the return of the results, often much larger, from the results we could see that the process mentioned just now is very important, the proportion of the pre-ranking process.

Here, we use the following command:

User@swd2:~$ Watch-n2 ' adb shell dumpsys meminfo zygote 'User@swd2:~$ Watch-n2 ' adb shell dumpsys meminfo mediaserver 'User@swd2:~$ Watch-n2 ' adb shell dumpsys meminfo drmserver 'User@swd2:~$ Watch-n2 ' adb shell dumpsys meminfo surfaceflinger 'User@swd2:~$ Watch-n2 ' adb shell dumpsys meminfo servicemanager ''

That is, every two seconds, the memory information of the specified process. Taking Zygote as an example, the results are returned as follows:

We focus on the total value of PSS, where 1628kB is the size of zygote.

In the same way, the size of MediaServer, Drmserver, Surfaceflinger and ServiceManager can be obtained, and all of them are calculated into frameworks, Can make the value of the framework more accurate and close to the actual real value.

In this example, all results obtained by the calculation are as follows:

Free
Product Perso Modem Frame Buffer Kernel System Service Frameworks Applications
Item X Zz 24 11 27 34 36.6 77.1 302.3

At this point, our assessment is complete. However, this is only a general grasp of the current situation of RAM, and the specific application of the state, but also need to be specifically analyzed.

6. Advantages

Concise sentence, is the core algorithm and evaluation methods, higher accuracy, more stable and reliable, close to the real situation.

Accuracy, because this method uses statistical values over a period of time, and is superior to the floating values in real-time conditions.

Stable and reliable, because the statistic value can effectively avoid the huge error of the floating value, so that each calculation result is kept within the small fluctuation range.

Closer to the real situation, it is because of the frameworks value of the school excellent. All unified use of PSS value is also more consistent with the calculation requirements.

The following is illustrated by the comparison of traditional meminfo methods:

It can be seen that the results obtained by using the Meminfo method are very discrete and the dispersion degree is significantly reduced by using the optimal evaluation scheme.

7. Expansion and application

This core idea and algorithm can also provide useful thoughts on the implementation of RAM management tools.

There are several ways to count memory values in the Android development framework, which require code modification or as a way to expand in the future.

    • [Email protected]

Ram Evaluation Optimization Scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.