banner
jzman

jzman

Coding、思考、自觉。
github

Android Multi-process Running Mechanism and IPC

Today, let's summarize the Android multi-process running mechanism and IPC introduction. The content is as follows:

  1. Processes in Android
  2. Introduction to Android IPC
  3. Enabling multi-process mode
  4. Android multi-process running mechanism

Processes in Android#

Firstly, a process can be understood as an independently running program. When a program is launched, 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 multiple processes. Each process runs in its own independent space. Android allocates a virtual machine for 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 interact directly 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 where background-executing Services are located.
  • Background process: A process relative to the foreground process.
  • Empty process: A process that can be understood as a cache, and 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 achieve inter-process communication. For example, on Windows, inter-process communication can be achieved through 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 IPC mechanism in Android, it generally refers to the Binder mechanism in Android.

Scenarios for using IPC:

  1. Some applications need to adopt 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.
  2. 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 initial memory allocation is 16M. The specific configuration is in the system/build.prop file.

Enabling multi-process mode#

In Android, multi-process mode can be enabled by configuring the android:process attribute in the four major components. It can be configured as a private process or a global process, as follows:

<!-- 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, the components of other applications cannot run in the same process. If configured as a global process, two applications can be set to the same ShareUID and run their components in the same process. In addition, the signatures of these two applications must be the same. This way, the components of the two applications 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?

  1. Set the same ShareUID for the two applications.
  2. Set the processes of the two components of the two applications to the same global process with the same process name.
  3. The signatures of the two applications are the same.

During testing, it was found that if a process has already been started and a 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 allocates a separate virtual machine for 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. In other words, there are two copies of the same class in two different 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 in Android share data through memory, the sharing will fail. The problems caused by multi-process are as follows:

  1. Static members and singleton pattern completely fail

In multi-process, there are multiple copies of a class, and modifications do not affect each other, so they naturally cannot take effect.

  1. Thread synchronization mechanism completely fails

The thread synchronization mechanism locks on different objects, so it naturally cannot take effect.

  1. Reliability of SharePreference decreases

Although SharePreference 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 SharePreference in multi-process is unreliable. The solution is to use ContentProvider as an intermediate layer to support SharePreferences in multi-process.

Specifically, other processes can access another process through ContentProvider, and the data is stored in SharePreference. The data can be operated by implementing insert, delete, query, and update methods in ContentProvider.

  1. 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 components running in the same process belong to the same virtual machine and the same Application, while components in different processes belong to multiple 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 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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.