1. Benefits of multiple processes

1. Increase memory usage to avoid OOM problems;

2. Scattered memory usage to reduce the probability of App being recycled;

(The Android system has a limit on how much memory can be allocated to each process, and processes that consume more memory are usually more likely to be killed by the system. Running a component in a separate process can reduce the memory footprint of the main process, avoid OOM problems, and reduce the probability of being killed by the system.

3. The child process crashes and the main process can continue working.

4, the main process exits, the child process can still work, for example, the child process is a push service.

5. Process survival can be achieved to a certain extent

2. Process classification

Android has two types of private and public processes

2.1 Private Processes

Android :process=”:remote”, starting with a colon, and optionally specifying the string after the colon. If our package is called “com. Demo. Multiprocess”, is actually the process name for “com. Demo. Multiprocess: remote”. This setting indicates that this process is the private process of the current application and components of other applications cannot run in the same process with this process.

2.2 Public Process

A process whose name does not start with a “:” can be called a global process. For example, android: Process =”com.secondProcess”, which starts with a lowercase letter, indicates that it is running in a global process with the same name. Other applications can run in the same process by setting the same ShareUID.

2.3 ShareUserId

In Android, each app has a unique Linux user ID, so that the application files (data directory) are only visible to the user. If you want to see other applications, you need to use SharedUserId. This means that both APKs use the same userID so that files can be shared.

2. To save resources, apKs with the same user ID can also run in the same process (note that they do not have to run in the same process) and share a virtual machine. ShareUserId is used to share data and call other program resources.

SharedUserId = sharedUserId; sharedUserId = sharedUserId

The sharedUserId is the same, but the process attribute is different: The system runs in different processes

Same sharedUserId, same process property: Run in the same process

3. Problems with multiple processes

Android assigns an independent VM to each process. Different VMS have different memory allocation addresses. As a result, multiple copies of the same class are generated when accessing the same class from different VMS. The problems are as follows:

3.1 Application Created multiple times

Creating a new process allocates an independent VIRTUAL machine. The process of starting a process is actually the process of starting an Application, which naturally causes multiple applications to be created.

The onCreate method of Application is generally used for initialization. In order to ensure the short initialization time and correctness of a new process, only necessary initialization can be performed according to the process name

3.2 Static Member Invalidation

Process empty memory is isolated, and changes in one memory space do not affect the other memory space

3.3 File Sharing Problems

If two processes access the same file at the same time, database corruption and data corruption may occur. Solution: 1, the child process calls the main process for file operation; 2. Different processes cannot use file storage (such as log, MMKV, etc.)

4. Process classification

1. Foreground process: the process of the Service that can interact directly with the user or is bound to the foreground.

2. Visible process: visible to the user but not clickable;

3. Service process: the process where the Service executed in the background resides.

4. Background process: the process in which the components in the process step back to the background.

5. Empty process: understood as a process in the sense of cache, the system can choose to recycle empty process at any time.

Its priority is Foreground process > Visible Process > Service process > Background process > Empty process.