Android process management: How to terminate the process during development

Android process management: How to terminate the process during development

In Android, directly killing the application process is usually not a recommended practice, but sometimes you may need to kill the corresponding process for certain specific needs (such as debugging or managing applications).

  1. 「Use android.os.Process.killProcess() method」: Use android.os.Process.myPid() method to get the ID of the current process, and then use android.os.Process.killProcess() method to kill the process. Due to Android's security mechanism, only processes with the same UID can kill each other. So this method can only be used for suicide, that is, to kill the calling process.
 int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid);
  1. 「Use System.exit() method」: You can terminate the currently running Java virtual machine, thereby terminating the program. When the Java virtual machine is terminated, all running threads will be stopped immediately, including non-daemon threads, and the resources occupied by activities will also be released. Calling this method will only terminate the current Java virtual machine and will not directly affect other Android processes.
 System.exit(0);

or:

 private static final int MSG_DELAY_EXIT_APP = 0; private static Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case MSG_DELAY_EXIT_APP: Runtime.getRuntime().exit(0); break; } } }; mHandler.sendEmptyMessageDelayed(MSG_DELAY_EXIT_APP, 4000);
  1. "Use ActivityManager.killBackgroundProcesses() method": You can force close all background processes associated with the specified package name (it will not kill the foreground process), and it will only work when system resources are tight, and the KILL_BACKGROUND_PROCESSES permission is required. This method can only be used for killing others, that is, killing the processes of other applications, and cannot be used for suicide.
 String packageName = getPackageName(); // 获取当前应用的包名ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); am.killBackgroundProcesses(packageName);

The Android system has its own strategy for managing processes and memory. If there is enough memory, Android will not kill any process at will; but if there is insufficient memory, the process may be killed at any time. When there is enough memory, Android will try to restore the previously killed process. For application developers, you should try to avoid relying on static variables to store important data, but should save the data to files or other persistent storage. At the same time, you also need to pay attention to the reasonable management of the application's memory usage to avoid the process being killed by the system due to problems such as memory leaks.

<<:  ActivityThread and ApplicationThread, the bridge between the main thread of the Android application and AMS communication

>>:  LinearLayoutCompat makes your Android linear layout more compatible, flexible and consistent

Recommend

WeChat ID can be changed at will? Official: It's just a bug

Recently, there have been a lot of news about WeC...

Undrinkable "milk" + seafood waste = national treasure?

The Zhejiang Provincial Museum houses several anc...

“Don’t stick your smartwatch on the high-speed train window”, why?

Review expert: Zhou Hongzhi, senior laboratory te...

What features did iOS 12 and Android 9.0 learn from each other?

Recently, the latest version of Android 9.0 has b...

The dust in the house can never be swept clean, what’s going on?

When you vacuum, sweep the floor, and wipe the fu...

Qualcomm and Huawei are increasingly competing for the first 5G chip

Qualcomm, which was overshadowed by Huawei's ...

The reward war escalates into a lose-lose game between Apple and APP developers

Following WeChat, Apple has finally taken action ...