How does "Android" make an Android app not be killed? Finishing

Source: Internet
Author: User

[Reprint] How to make an Android app not be killed? Finishing Original Address:How to make an Android app not be killed? Finishing Drifting brocade Maple Reference: http://blog.csdn.net/windskier/article/details/6560925
http://blog.csdn.net/zmyde2010/article/details/6756368
Http://blog.sina.com.cn/s/blog_514048cb0100wi2j.html

Method:
For a service, you can first set it to run in the foreground:
public void Myservice.oncreate () {
Super.oncreate ();
Notification Notification = new Notification (Android. R.drawable.my_service_icon,
"My_service_name",
System.currenttimemillis ());
Pendingintent p_intent = pendingintent.getactivity (this, 0,
New Intent (this, mymainactivity.class), 0);
Notification.setlatesteventinfo (This, "myservicenotification," Myservicenotification is running! ", p_intent);
LOG.D (TAG, String.Format ("notification =%s", notification));
Startforeground (0x1982, notification); Notification id:0x1982, you can name it as you'll.
}

-------------------------------
Applications placed under/system/app enjoy more privileges than/data/app applications, such as setting the persistent property to true in their manifest.xml files, which can be protected from out-of-memory killer. Androidmanifest.xml files such as the application ' Phone ':
<application android:name= "Phoneapp"
Android:persistent= "true"
Android:label= "@string/dialericonlabel"
android:icon= "@drawable/ic_launcher_phone" >
...
</application>
After setting the app to the core level of the system, in any case will not be killed, settings->applications inside will also block the stop operation.

This sets the previous Log:proc #19: adj=svc/b 4067b028 255:com.xxx.xxx/10001 (started-services) # Cat/proc/255/oom_adj 4 after setting the LO G:pers #19: adj=core/f 406291f0 155:com.xxx.xxx/10001 (fixed) # cat/proc/155/oom_adj-12 # this is Cor E_server_adj
Note: The Oom_adj of the INIT process is-16 (that is, System_adj): Cat/proc/1/oom_adj
Android related section analysis:
The following code is in the file Frameworks/base/services/java/com/android/server/am/activitymanagerservice.java:
Final Processrecord addapplocked (applicationinfo info) {
Processrecord app = getprocessrecordlocked (Info.processname, Info.uid);

if (app = = null) {
App = newprocessrecordlocked (null, info, NULL);
Mprocessnames.put (Info.processname, Info.uid, app);
Updatelruprocesslocked (app, True, true);
}

if (info.flags& (applicationinfo.flag_system| Applicationinfo.flag_persistent))
= = (applicationinfo.flag_system| Applicationinfo.flag_persistent)) {
App.persistent = true;
App.maxadj = Core_server_adj; This constant value is-12.
}
if (App.thread = = null && mpersistentstartingprocesses.indexof (APP) < 0) {
Mpersistentstartingprocesses.add (APP);
startprocesslocked (App, "added application", app.processname);
}

return app;
}

If you want to be a CORE service (that is, App.maxadj = Core_server_adj (-12)), the application requires Flag_system and flag_persistent two flags, and Flag_system refers to the application located/ System/app, flag_persistent refers to the persistent attribute.

For Frameworks/base/services/java/com/android/server/systemserver.java, the call
Activitymanagerservice.setsystemprocess ();
Set your own App.maxadj to System_adj, i.e.-16.

principle:
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:
1) Recycling rules: When to recycle and recycle which one?
2) Avoid manslaughter: how can I prevent it from being recycled?
3) Data recovery and storage: What if it is recycled?

Android divides the process into 6 levels, which are ranked from highest to lowest in order of precedence:
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 at least as important as the B process.
3. The phone service in the system is divided into the foreground process rather than the secondary service process.

In Android, the Oom_adj value of a process also represents its priority. A higher Oom_adj value means that the process has a lower priority. The following property settings are in the file/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 of the PID 1 process (init process) is set to System_adj (-16):
# Set init its forked children ' s oom_adj.
Write/proc/1/oom_adj-16

To view native settings:
Cat/sys/module/lowmemorykiller/parameters/adj
0,1,2,7,14,15

Recycling Time:
In file/init.rc:
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
These numbers are the corresponding memory thresholds, and once they are below this value, Android starts to turn off the corresponding level of process in order.
Note The units of these numbers are page:1 page = 4 KB. So the above six numbers correspond to (MB): 6,8,16,20,22,24.

To view the current memory threshold settings:
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 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 specific recycling implements the function Trimapplications () in Activitymanagerservice.java:
1. First remove the useless process that the package has been uninstalled;
2. Update the OOM_ADJ value based on the current status of the process, and then do the following:
1) Remove the process that has no activity in operation;
2) 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:
The Oom_adj of the process is computed in the computeoomadjlocked () of the Activitymanagerservice.java file, 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 the system is in short supply) to free its ram, and the source code is in KERNEL/DRIVERS/MISC/LOWMEMORYKILLER.C. Simply put, it is looking for the most appropriate process to kill, freeing up the memory it occupies.
The most appropriate process is:
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 operation takes the form of service running in the foreground, because a process running a 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, which requires the reload of the backend button (no activity is first killed in the running process).
* Reliance on other high-priority processes;

2) Force modify process properties:
* Set in Process: Setpersistent (TRUE);
* Set in the manifest file (as above).

How does "Android" make an Android app not be killed? Finishing

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.