Http://blog.sina.com.cn/s/blog_40d475e901012jgt.html
Processes in Android are managed, and when the system process is tight, the process is automatically recycled according to the priority level.
This brings three questions:
L Recycling Rules: When to recycle and recycle which one
L Avoid manslaughter: How to prevent being recycled
• Data Recovery and preservation: What to do if it is recycled
Android divides the process into 6 levels, in order of precedence from high to low
In turn:
1. Foreground process (Foreground_app)
2. Visual process (Visible_app)
3. Secondary service process (Secondary_server)
4. Background process (Hidden_app)
5. Content Provisioning Node (content_provider)
6. Empty process (Empty_app)
Characteristics:
1. If a process contains both service and visual activity, the process should be attributed to the visual process, not the service process.
2. In addition, the level of a process can be improved if other processes depend on it. For example, a service in a process is bound to a component in the B process, and process A will always be considered as important as the B process at least.
3. The phone service in the system is divided into the foreground process rather than the secondary service
Process.
The Oom_adj value of the process in Android also represents its priority.
A higher Oom_adj value means that the process has a lower priority. In init.rc:
setprop ro. Foreground_app_adj 0
setprop ro. Visible_app_adj 1
setprop ro. Secondary_server_adj 2
setprop ro. Hidden_app_min_adj 7
setprop ro. Content_provider_adj 14
setprop ro. Empty_app_adj 15
Init.rc, the Oom_adj setting of the PID 1 process (init process)
To-16:
To view native settings:
Cat/sys/module/lowmemorykiller/parameters/adj
0,1,2,7,14,15
Recycling Time:
In init.rc:
setprop ro. Foreground_app_mem 1536
setprop ro. Visible_app_mem 2048
setprop ro. Secondary_server_mem 4096
setprop ro. Hidden_app_mem 5120
setprop ro. Content_provider_mem 5632
setprop ro. Empty_app_mem 6144
These numbers are the corresponding memory thresholds, and once they are below this value, Android starts to close the process in order.
Note that the units for these numbers are page. 1 page = 4 KB.
The above six numbers correspond to (MB): 6,8,16,20,22,24
View the settings now to:
Cat/sys/module/lowmemorykiller/parameters/minfree
To reset the value (corresponding to different requirements):
echo "1536,2048,4096,5120,15360,23040" >/sys/module/
Lowmemorykiller/parameters/minfree
This starts the end of the empty process when the available memory is below 90MB. When the available memory is less than 60MB, the content supply node begins to end.
Specific recycling implemented in Activitymanagerservice.java function trimapplications ()
1. First remove the useless process that the package was removed from.
2. Update the OOM_ADJ value based on the current status of the process, and then do the following.
Remove a process that has no activity running
If the AP has saved all activity states, end this AP.
3. Finally, if there are still many activities running, remove the activity that has been saved by the activity State.
To update the value of Oom_adj:
Calculate the Oom_adj of a process in computeoomadjlocked, for example:
if (app = = Top_app) {
The last app in the list is the foreground app.
Adj = Foreground_app_adj;
App.adjtype = "Top-activity";
}
Low memory killer in Android kernel
Android's low memory killer kills processes as needed (when system memory is scarce), and the source code
Kernel/drivers/misc/lowmemorykiller.c
Simply put, find the most appropriate process to kill, freeing up the memory it occupies.
Most suitable:
The bigger the Oom_adj
• The more physical memory is consumed
Once a process is selected, the kernel sends a sigkill signal to kill it.
For_each_process (p) {
......
if (selected = = NULL | | P->oomkilladj > SELECTED->OOMKILLADJ | |
(P->oomkilladj = = Selected->oomkilladj &&
Tasksize > Selected_tasksize))
{
selected = P;
}
}
if (Selected! = NULL) {
Force_sig (SIGKILL, selected);
}
View LRU list: Adb shell Dumpsys activity
When Activitydemo is at the front desk:
The process that contains the service has a high priority, and in computeoomadjlocked it is divided into two small classes:
static final int max_service_inactivity = 30*60*1000;
if (now < (s.lastactivity+max_service_inactivity)) {
if (Adj > Secondary_server_adj) {
Adj = Secondary_server_adj;
App.adjtype = "Started-services";
App.hidden = false;
}
}
if (Adj > Secondary_server_adj) {
App.adjtype = "Started-bg-services";
}
}
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:
1. Increase the priority of the process:
Background operations take the form of service because a process running a service is a higher level 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.
To force modify process properties:
• Set Setpersistent (True) in the program;
• Add attribute Android:persistent= "true" to Androidmanifest.xml <application> in project
Android:persistent (SDK)
Whether or not, the application should remain running at all times. The default value is "false". Applications should not normally set this flag; Persistence mode is intended only for certain system applications (Phone,system).
1. Use killprocess (int pid) to kill the process of the specified PID
public void OnClick (View v) {
Android.os.Process.killProcess (Android.os.Process.myPid ());
}
Use System.exit (0) to achieve the same effect.
2. Use Finish () to kill the current activity
1. Save the information: It is best to save the data each time the activity runs to the OnPause or OnStop state, and then read the data (life cycle method) at OnCreate.
2. Onsaveinstancestate (non-life cycle method)
Onrestoreinstancestate
Transfer from http://blog.sina.com.cn/s/blog_514048cb0100wi2j.html