Let Android apps not be killed (killer) method _android

Source: Internet
Author: User

Method:
for a service, you can first set it to run in the foreground:

Copy Code code as follows:

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, can name it as you.
}

-------------------------------
Compared with the application under/data/app, the application under/system/app enjoys more privileges, for example, if you set the Persistent property to true in its manifest.xml file, you can protect it from out-of-memory killer. such as the Androidmanifest.xml file for application ' Phone ':
Copy Code code as follows:

<application android:name= "Phoneapp"
Android:persistent= "true"
Android:label= "@string/dialericonlabel"
android:icon= "@drawable/ic_launcher_phone" >
...
</application>

After setting up the app to the system core level, under no circumstances will not be killed, settings->applications inside will also block out the stop operation.

Log in front of this setting:

Copy Code code as follows:

Proc #19: adj=svc/b 4067b028 255:com.xxx.xxx/10001 (started-services)
# Cat/proc/255/oom_adj
4

Log after setting:
Copy Code code as follows:

PERS #19: adj=core/f 406291f0 155:com.xxx.xxx/10001 (fixed)

# Cat/proc/155/oom_adj
-12 # This is Core_server_adj

Note: The Oom_adj of the INIT process is-16 (i.e. System_adj): Cat/proc/1/oom_adj

Android Related Parts Analysis:

The following code is available in the file Frameworks/base/services/java/com/android/server/am/activitymanagerservice.java:

Copy Code code as follows:

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;
}


Visible to be a CORE service (i.e. App.maxadj = Core_server_adj (-12)), the application needs Flag_system and flag_persistent Two flags, Flag_system refers to the application located in/ System/app, flag_persistent refers to the persistent attribute.

And for Frameworks/base/services/java/com/android/server/systemserver.java, the call to the

Copy Code code as follows:

Activitymanagerservice.setsystemprocess ();

Set your own App.maxadj into System_adj, that is, 16.

Principle:

Processes in Android are hosted, and processes are automatically recycled when the system is in a tight space. This brings three questions:
1 Recycling rules: When to recycle and recycle which one?
2 Avoid manslaughter: How to prevent being recycled?
3 Data Recovery and Save: What to do if it is recycled?

Android divides processes into 6 levels, 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 Supply Node (content_provider)
6. Empty process (Empty_app)

Features:

1. If a process contains both service and visual activity, the process should be attributed to the visual process rather than 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.

In Android, the Oom_adj value of a process also represents its priority. The higher the Oom_adj value represents the lower the process priority. The following property settings are available in file/init.rc:

Copy Code code as follows:

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

In/init.rc, the Oom_adj of the process (init process) with PID 1 is set to System_adj (-16):
Copy Code code as follows:

# Set init its forked children ' s oom_adj.
Write/proc/1/oom_adj-16

To view the native settings:
Copy Code code as follows:

Cat/sys/module/lowmemorykiller/parameters/adj
0,1,2,7,14,15

Time to recycle:
In file/init.rc:
Copy Code code as follows:

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 that, Android begins to turn off the process in order.

Note that the units of these numbers are page:1 page = 4 KB. So the above six digits correspond to (MB): 6,8,16,20,22,24.

To view the current memory threshold settings:

Copy Code code as follows:

Cat/sys/module/lowmemorykiller/parameters/minfree

To reset the value (corresponding to different requirements):
Copy Code code as follows:

echo "1536,2048,4096,5120,15360,23040" >/sys/module/lowmemorykiller/parameters/minfree

This kills the "empty process" when the available memory is less than 90MB and starts killing the content Supply node class process when the available memory is below 60MB.

The specific recovery implementation in Activitymanagerservice.java function Trimapplications ():
1. First remove the unwanted process that package has been unloaded;
2. Update the OOM_ADJ value based on the current state of the process, and then do the following:
1 removal of the process without activity in operation;
2 If the AP has saved all activity states, end this AP.
3. Finally, if there are still a lot of activities running, remove the activity that the activity state has preserved.

To update Oom_adj values:
The Oom_adj of the process is calculated in the computeoomadjlocked () of the Activitymanagerservice.java file, for example:

Copy Code code as follows:

if (app = Top_app) {
The "last app" on the "list is" the foreground app.
Adj = Foreground_app_adj;
App.adjtype = "Top-activity";
}

Low memory killer in Android kernel
The android low Memory killer kills the process to release its memory as needed (when system memory is scarce), and the source code is in KERNEL/DRIVERS/MISC/LOWMEMORYKILLER.C. Simply put, it is to look for the most appropriate process to kill, thus 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:
Copy Code code as follows:

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 higher priority and is divided into two small classes in computeoomadjlocked:
Copy Code code as follows:

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 impossible to completely keep the process from being killed, and we can make it less likely to kill the process by doing something:
1 increase the priority of the process:
* Backstage operation uses the service form which runs in the foreground, because one runs the service the process to run the backstage activity the level to be high;
* Press the back key to enable activity in the process to run in the background instead of Destory, overload the Back button (no activity in the running process is first killed).
* reliance on other high priority processes;

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

Related Article

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.