Reference article:
Http://www.apkbus.com/android-104940-1-1.html
Http://blog.sina.com.cn/s/blog_3e3fcadd0100yjo2.html
First, the theory:
Android takes a different process management strategy from Linux, and unlike Linux, which ends the process after the process activity has stopped, Android keeps the processes in memory until the system needs more memory. These processes, which are kept in memory, typically do not affect the speed of the overall system, and increase the startup speed of the process when the user activates the processes again.
when does Android end the process? What is the end of the process? How do I stop the end?
Android Memory management mechanism:
1. The importance of the process is evaluated and the importance is expressed in terms of the value "Oom_adj", which is given to each process (the system will determine which processes need to end according to "Oom_adj"), in general, the greater the value of "Oom_adj", The process is selected by the system to terminate the higher possible)
2. The "Oom_adj" value of the foreground program is 0, which means that it will not be terminated by the system, and once it is inaccessible, a higher "Oom_adj" is obtained, and the author speculates that the value of "Oom_adj" is determined by the location of the software in the LRU list;
3.Android is different from Linux, has its own unique process Management module, this module is more customizable, can be based on the "Oom_adj" value of the scope to determine the process management policy, such as can be set "when memory is less than X, the end of" Oom_adj "greater than Y process". This gives the process management script the option to write more.
Second, Android divides the process into six categories ( listed by process priority ):
1. Foreground process (foreground): The process currently being displayed on the screen and some system processes. For example, a system process such as Dialer Storage,google Search is the foreground process, and for example, when you run a program, such as a browser, when the browser interface is displayed in the foreground, the browser belongs to the foreground process (foreground). But once you press home to return to the main screen, the browser becomes a daemon (background). The last process we want to terminate is the foreground process.
2. Visible process (visible): visible process is some no longer the foreground, but the user is still visible process, for example: Widgets, Input method, etc., are visible. Although this part of the process is not in the foreground, but also closely related to our use, we do not want them to be terminated (you certainly do not want the clock, weather, news and other widgets are terminated, then they will not sync, you do not want the input method is terminated, or you need to restart input input every time)
3. Secondary services (secondary server): Some of the services currently running (major services, such as dialing, are not likely to be terminated by process management, so this is only a minor service), for example: Google Enterprise Suite, gmail internal storage, contact internal storage and so on. Although this part of the service is a secondary service, but some of the system functions are still closely related, we often need to use them, so we also hope they are terminated
4. Background process (background): Although the author uses the word hidden, but actually is the background process (background), we usually understand the start after the switch to the background of the process, such as browsers, readers and so on. When the program is displayed on the screen, the process that he runs is the foreground process (foreground), and once we press home to return to the main interface (note that by pressing home, not pressing back), the program resides in the background and becomes the background process
(background). Background process Management policy has a variety of: in a more positive way, once the program arrives in the background immediately terminate, this way will increase the speed of the program, but can not speed up the program to start again, there are more negative ways, as much as possible to retain the background program, although it may affect the speed of a single program, However, the speed is increased when you start a program that has started again. This will require users to find a balance based on their own usage habits.
5. Content provider: There is no program entity, in the provision of content for other programs to use, such as Calendar supply node, mail supply node. This type of procedure should have a higher priority when the process is terminated
6. Null process (empty): There is nothing in the process of running, some programs, such as BTE, after the program exits, will still reside in the process of an empty process, the process is not any data running, the role is often to improve the program's next boot speed or record some of the history of the program information. This part of the process is undoubtedly the first to be terminated.
Third, actual combat:
1, view the current system of various types of process thresholds :
Execute with Root: Cat/sys/module/lowmemorykiller/parameters/minfree
The display should be 6 numbers separated by commas, for example: 1536,2048,4096,5120,5632,6144
Note that the units for these numbers are page. 1 page = 4KB; the above six numbers correspond to (MB): 6,8,16,20,22,24. These numbers are the corresponding memory thresholds, and once they are below this value, Android starts to close the process sequentially. So Android starts to end with the lowest priority Empty_app when available memory is less than 6MB (1536*4/1024).
However, there are some problems with the default settings :
Various processes of the management strategy of the threshold is quite close: 6,8,16,20,22,24, the maximum difference is less than 8MB, in the actual program running, it is easy to cause a variety of types of processes are closed at the same time. If the availability of 25 o'clock, the sudden start of the camera program, the system can be rapidly available memory, can lead to empty processes, content supply nodes, background processes, secondary services, etc. are closed at the same time, lower thresholds: General mobile phone After the start, the availability of 50-100 or so, but with the use of mobile phones, the available memory will gradually reduce, Finally reduced to about 24MB, then the system began to start the process management mechanism, began to end the process, but the valve limit is set at 24MB, relatively low. The result is that the overall speed of the system slows down after a period of time. Obviously, when the phone is used for a long time, the system reacts very slowly when the phone dials, albums, cameras and other applications are turned on.
Based on the above problems, it is not difficult to see that our revised target will be very clear, the main solution to two contradictions :
The threshold level of each process is opened , so that the process management mechanism can work more effectively.
increase the upper threshold, empty more free memory, to improve the overall system operation speed
How do you pull the threshold for different processes?
Execute command:
echo "1536,2048,4096,5120,15360,23040" >/sys/module/lowmemorykiller/parameters/minfree
This will kill the "empty process" when the available memory is below 90MB, and start killing the "Content supply node" class process when the available memory is below 60MB.
The value changed by the above method is not permanent. After the next restart, the previous settings are restored . To allow Setup to execute on each boot, we can add the following to/SYSTEM/INIT.D :
SetProp ro. Foreground_app_mem 1536//6M
SetProp ro. Visible_app_mem 2048//8M
SetProp ro. Secondary_server_mem 4096//16M
SetProp ro. Hidden_app_mem 5120//20M
SetProp ro. Content_provider_mem 5632//22.4M
SetProp ro. Empty_app_mem 6144//24M
Process Management policy setting principles:
①, foreground processes, visible processes, and secondary services are content that is relevant to the user experience, and this part of the process management strategy is relatively conservative, leaving enough room for these processes to run
②, squeezing useless processes, freeing up memory space for the main program to use
It is not possible to completely keep the process from being killed , and we can make the process less likely to kill by doing something:
①, increase the priority of the process : The background operation takes the service form running in the foreground, because a process running the service is higher than a running background activity;
Pressing the back key causes the activity in the process to run in the background instead of the destory, and it needs to be reloaded (without any activity in the running process being first killed). Relies on other high-priority processes.
②, Force Modify process Properties : Set in Process:setpersistent (True) or set in manifest file:android:persistent= "true"
Android Memory process Management mechanism