Help Center> Cloud Phone Host> User Guide> Using AOSP> Background Process Management
Updated on 2023-10-11 GMT+08:00

Background Process Management

Function

The cloud phone cph_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.

  • cph_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: cph_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

    View cph_performance information, enable or disable cph_performance features, and manage whitelists.

    adb shell dumpsys cph_performance [command] [args]

    Command

    Parameter

    Description

    Example

    debug

    N/A

    Enables or disables DEBUG logs.

    adb shell dumpsys cph_performance debug

    setEnableBgKiller

    [true] or [false]

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

    adb shell dumpsys cph_performance setEnableBgKiller true

    setEnableAutoRunController

    [true] or [false]

    Enables or disables background automatic startup management. This feature is enabled by default.

    adb shell dumpsys cph_performance setEnableAutoRunController true

    addBgKillerWhiteApp

    [packageNames]

    Package name list. Package names are separated by spaces.

    Adds a whitelist of background processes to be cleared.

    adb shell dumpsys cph_performance addBgKillerWhiteApp com.android.pkg1 com.android.pkg2

    rmBgKillerWhiteApp

    [packageNames]

    Package name list. Package names are separated by spaces.

    Deletes a whitelist of background processes to be cleared.

    adb shell dumpsys cph_performance rmBgKillerWhiteApp com.android.pkg1 com.android.pkg2

    addAutoRunWhiteApp

    [packageNames]

    Package name list. Package names are separated by spaces.

    Adds a whitelist of background apps that will automatically start.

    adb shell dumpsys cph_performance addAutoRunWhiteApp com.android.pkg1 com.android.pkg2

    rmAutoRunWhiteApp

    [packageNames]

    Package name list. Package names are separated by spaces.

    Deletes a whitelist of background apps that will automatically start.

    adb shell dumpsys cph_performance rmAutoRunWhiteApp com.android.pkg1 com.android.pkg2

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.