Today, let's summarize the Android multi-process running mechanism and IPC introduction. The content is as follows:
- Processes in Android
- Introduction to Android IPC
- Enabling multi-process mode
- Android multi-process running mechanism
Processes in Android#
Firstly, a process can be understood as an independently running program. When a program is started, the system will create a process for it and allocate the necessary system resources. At the same time, the process will be added to the process ready queue, and the process scheduler is responsible for running which process.
In Android, an application can be a single process or configured as a multi-process. Each process runs in its own independent space. Android assigns a virtual machine to each process, and different virtual machines have different address spaces for memory allocation. Different processes naturally involve inter-process communication.
The processes in Android are as follows:
- Foreground process: a process that can directly interact with the user or is bound to a foreground Service.
- Visible process: a process that is visible to the user but cannot be clicked.
- Service process: a process that executes background Services.
- Background process: a process relative to the foreground process.
- Empty process: a process that can be understood as a cache-like process. The system can choose to reclaim empty processes at any time.
The priority is foreground process > visible process > service process > background process > empty process.
Introduction to Android IPC#
IPC (Inter-Process Communication) refers to inter-process communication or cross-process communication. Any operating system needs a corresponding IPC mechanism to complete inter-process communication. For example, on Windows, inter-process communication can be achieved through the clipboard, pipes, etc. On Linux, inter-process communication can be achieved through channels, shared memory, semaphores, etc. Although Android is based on Linux, in order to adapt to the characteristics of mobile devices, it specifically provides a process communication method called Binder. Therefore, when it comes to the IPC mechanism in Android, it generally refers to the Binder mechanism in Android.
Scenarios for using IPC:
- Some applications need to use the multi-process mode. For example, in Android, there is a limit on the maximum memory that an application can obtain. In order to allow the application to obtain more memory space, some modules are placed in independent processes. In this case, inter-process communication is needed to complete the interaction between modules.
- The application itself needs to interact with other applications across processes. Whether it is using ContentProvider or AIDL, it belongs to the scope of IPC. For example, it is widely used in Android set-top box IPTV middleware.
Note: Different manufacturers have different restrictions on application memory in Android, and the default allocated initial memory is 16M. The specific configuration is in the system/build.prop file.
Enabling Multi-Process Mode#
In Android, enabling multi-process mode is done by configuring the android:process
attribute in the four major components. It can be configured as a private process or a global process, as shown below:
<!--Private process-->
<!--com.manu.progress:remote-->
<activity
android:name="com.manu.process.SampleActivity"
android:process=":remote"/>
<!--Global process-->
<!--com.manu.remote-->
<activity
android:name="com.manu.process.SampleActivity"
android:process="com.manu.remote"/>
If configured as a private process, components of other applications cannot run in the same process. If configured as a global process, two applications can be set to have the same ShareUID and run their components in the same process. In addition, the two applications must have the same signature, so that their components can run in the same process and share private data such as the data directory.
So how to configure the components of two applications in the same process?
- Set the same ShareUID for the two applications.
- Set the processes of the two components of the two applications to the same global process name.
- The two applications have the same signature.
During testing, it was found that if a process has already been started and another component configured with the same process is started, the second application will exit abnormally. The reason for this still needs to be further understood by reading the source code.
- How to configure the components of two applications in the same process?
Android Multi-Process Running Mechanism#
Android assigns a separate virtual machine to each process, and different virtual machines have different address spaces for memory allocation. This means that accessing the objects of the same class in different virtual machines will create multiple copies, that is, two copies of the same class exist in two processes, and these two classes do not interfere with each other. Modifying one will not affect the other. This leads to a problem: if the four major components running in different processes share data through memory, the sharing will fail. The problems caused by multi-process are as follows:
- Static members and singleton patterns are completely ineffective.
In multi-process, there are multiple copies of a class, and modifications do not affect each other, so they naturally do not take effect.
- Thread synchronization mechanism is completely ineffective.
The thread synchronization mechanism locks on different objects, so it naturally does not take effect.
- Reliability of SharePreferences decreases.
Although SharePreferences can be set to support multi-process with MODE_MULTI_PROCESS, it is not recommended to use it because it does not work in some versions and has been deprecated since API 23. Therefore, using MODE_MULTI_PROCESS to support SharePreferences in multi-process is unreliable. The solution is to use ContentProvider as an intermediate layer to access SharePreferences through ContentProvider to support multi-process.
Specifically, other processes can access another process through ContentProvider, and the data is stored in SharePreferences. The data can be operated by implementing insert, delete, query, and update methods in ContentProvider.
- Application is created multiple times.
When a component runs in a new process, the process creation process is actually the process of starting an application. Naturally, the Application will be created multiple times. It can be understood that the components running in the same process belong to the same virtual machine and the same Application, while the components in different processes belong to different virtual machines and Applications.
The onCreate method of Application is generally used for initialization operations. If the onCreate method of Application is called multiple times, to avoid errors, you can judge based on the process name and then perform relevant initialization.
The above is an introduction to the Android multi-process running mechanism and IPC. The implementation methods of IPC in Android include Bundle, file transfer, AIDL, Messenger, ContentProvider, etc. This part will be organized and shared in the future.