Help Center/ Cloud Phone Host/ User Guide/ Using AOSP/ Background Process Management
Updated on 2024-07-31 GMT+08:00

Background Process Management

Function

The cloud phone performance automatically clears background application processes. It prevents automatic startup and mutual wakeup of the third-party applications in the background. This improves the startup speed and reduces the occurrence of OOM and high CPU usage.

  • performance: low-memory detection and process killing policy
    When the remaining memory of a cloud phone is lower than a threshold, processes with different priorities are killed in descending order of the remaining memory size.

    Level

    Remaining Memory

    Processes to Be Cleared

    High

    25% of the total memory

    Empty processes

    Cache processes

    Middle

    20% of the total memory

    Used and unperceivable processes

    (invisible, no floating window, no audio focus, and more)

    Low

    15% of the total memory

    Activity processes that have been used last time

    Provider processes that have been used last time

    Background service processes

    Critical

    200 MB

    Kill all application processes in descending order of the memory used until the remaining memory is greater than 200 MB.

How to Use

  • Listening to the broadcast of the killed application
    Your background management program obtains the package name and cause why the application was killed by listening to the broadcast of the killed application.
    public static final String ACTION_APP_KILLED = "android.intent.action.APP_KILLED";
    
    private MyBroadcastReceiver mBroadcastReceiver = new MyBroadcastReceiver();
    
    private void registerReceiver() {
    	IntentFilter filter = new IntentFilter(ACTION_APP_KILLED);
    	this.registerReceiver(mBroadcastReceiver, filter);
    }
    
    private void unregisterReceiver() {
    	this.unregisterReceiver(mBroadcastReceiver);
    }
    
    private class MyBroadcastReceiver extends BroadcastReceiver {
    
    	private static final String TAG = "AppKilled";
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		Log.d(TAG, "package: " + intent.getStringExtra("package"));
    		Log.d(TAG, "reason: " + intent.getStringExtra("reason"));
    	}
    }
  • Example of memory leak in the foreground app

    The total memory of a cloud phone is 3.79 GB. Compile the demo app to request ultra-large memory. Use the Java layer to call the native layer through the Java Native Interface (JNI) so that the remaining memory of the system is less than 200 MB. As a result, processes corresponding to the high level to the critical level are killed.

    #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)
    #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
    extern "C" JNIEXPORT jint JNICALL
    Java_com_android_memnative_MainActivity_mallocMem(JNIEnv *env, jobject thiz, jint mb) {
        jint total = 0;
        for (jint i = 0; i < mb; i++) {
            jint size = sizeof(char) * 1024 * 1024;
            char *p = (char *) malloc(size);
            if (p != NULL) {
                memset(p, 1, size);
                LOGI("malloc success: %d mb\n", mb);
                total += 1;
            } else {
                LOGE("malloc failed: %d mb\n", mb);
            }
        }
        LOGI("malloc total: %d mb\n", total);
        return total;
    }
    public class MainActivity extends AppCompatActivity {
     
        static {
            System.loadLibrary("memnative");
        }
    
        public native int mallocMem(int mb);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.activity_main);
    
            mallocMem(3300); // malloc native memory 3300MB
        }
    }

    The system automatically clears the application processes based on the process importance and sends a broadcast of the application killed. After the background processes are cleared, if the remaining memory is still less than 200 MB and there is a foreground app that uses the largest amount of memory, the foreground app will be killed and a low memory notification is displayed in the notification bar.

  • Background automatic startup management policy
    Except for the processes of the applications specified in the following exemption policies, third-party application processes are not allowed to automatically start or wake up each other in the background.

    Application to Which Exemption Policies Apply

    Example

    System applications

    /system/app, /system/priv-app, and more

    Privileged applications

    uid=1000 (system processes), the default input method, and more

    Visualization applications

    Apps with desktop widgets and live wallpapers

    Whitelisted applications

    Refer to the command line tool.

  • Key Logcat logs
    • Key logs of low memory detection and process killing
      logcat | grep LMK
      BackgroundAppKiller: [LMK] start to kill top app, which takes up a lot of memory: ActivityRecord{248080f u0 com.android.memnative/.MainActivity t84}
      BackgroundAppKiller: [LMK] Killing AppInfo{packageName='com.android.memnative', uid=10059, isHighMemClean=false, reason='[memory:critical]', startTraffic=0, delayMillis=0, totalMillis=0}
      lowmemorykiller: [LMK] start to kill process.
      lowmemorykiller: [LMK] Killing 'com.android.deskclock' (4198), uid 10049, adj 906

      BackgroundAppKiller: performance

      lowmemorykiller: native AOSP low memory killer daemon (lmkd) service

    • Viewing logs of processes that are automatically started in the background and intercepted
      logcat -s AutoRunController | grep "has no auto-run permission"
      AutoRunController: com.android.memnative has no auto-run permission to startProcess

  • Command line tool
    • adb shell cmd performance [command] [args]
      You can use this command to enable or disable a performance feature of CPH, add a whitelist, or delete a whitelist.

      Command

      Parameter

      Description

      Example

      bg-killer

      -e <true|false>

      Enables or disables background process clearing. This feature is enabled by default.

      adb shell cmd performance bg-killer -e true

      -a <packageName|packageNames>

      Adds a whitelist of background processes to be cleared.

      Use commas (,) to separate multiple package names.

      adb shell cmd performance bg-killer -a com.android.test1,com.android.test2

      -d <packageName|packageNames>

      Deletes the whitelist of background processes to be cleared.

      Use commas (,) to separate multiple package names.

      adb shell cmd performance bg-killer -d com.android.test1,com.android.test2

      auto-run

      -e <true|false>

      Enables or disables automatic startup of background third-party applications. This feature is enabled by default.

      adb shell cmd performance auto-run -e true

      -a <packageName|packageNames>

      Adds a whitelist of third-party applications that will automatically start in the background.

      Use commas (,) to separate multiple package names.

      adb shell cmd performance auto-run -a com.android.test1,com.android.test2

      -d <packageName|packageNames>

      Deletes the whitelist of third-party applications that will automatically start in the background.

      Use commas (,) to separate multiple package names.

      adb shell cmd performance auto-run -d com.android.test1,com.android.test2

    • adb shell dumpsys performance [command] [args]

      You can use this command to view whether a performance feature of CPH is enabled or not and to view a whitelist.

Constraints

  • Background process management is enabled by default.
  • By default, daemon processes and processes of whitelisted applications are exempted.
  • All running application processes, including those of the whitelisted applications, at the critical level of low memory, will be killed.