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

Jobs' Fence

[[136824]] How did Jobs develop his pursuit of su...

NetEase H5 screen-sweeping skills and strategies!

During this year's National Day, NetEase main...

2021 Huoyanshe Pinduoduo Operation Course Collection

"2021 Huoyanshe Pinduoduo Operation Course&q...

12 Practical Ways to Prevent Your App from Becoming a Zombie

Are you familiar with the concept of "zombie...

Zhihu traffic mining methodology: How to mine most effectively?

Doing well on Zhihu is equivalent to doing well o...

How to operate activities? These 7 creative ideas give you the answer

Marketing and promotion is an area of ​​knowledge...