How to use multi-process, multi-process benefits, multi-process defects, and solutions in Android development

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/spencer_hale/article/details/54968092

1. How to use multi-process

Android Multi-process concept: In general, an application is a process, and the name of the process is the application package name. We know that processes are the basic unit of system allocation of resources and dispatch, so each process has its own independent

Resources and memory space, other processes cannot arbitrarily access the memory and resources of other processes.

How to get your app to have multiple processes:

Four components when registering in the Androidmanifest file, there is a property android:process where you can specify the process in which the component is located.

The default is the app's main process. When the system starts this component, it is created (if it has not yet been created), and then the component is created before it is specified as another process. Print out its process name: Heavy

The OnCreate method of application class can be downloaded.

To set the Android:process property, note: If it is android:process= ":d Eamon" to: The first name indicates that this is a private process for an application, otherwise it is a global process. The process name of the private process is

The package name is automatically added before the colon, and the global process does not. Generally we have private processes and seldom use global processes.

2. Benefits of using multiple processes

Benefits:

(1) share the memory pressure of the main process.

As the application grows larger and more memory is used, the separate components are placed in different processes, and it does not occupy the memory space of the main process. Of course there are other benefits, and the conscientious will find

(2) To keep the application in the background, prevent the main process is killed Daemon, the daemon and the main process of monitoring each other, a party was killed to restart it.

There are many applications in the Android daemon process, because they are in the background, especially for instant messaging or social applications, but now many processes have been used to rot.

The typical usage is to start an invisible lightweight private process, send and receive messages in the background, or do some time-consuming things, or start the process on boot, and then do the listening.

#坏处: Consumes the user's power.

It takes up a lot of space in the system, and if all the applications are so occupied, the system memory can easily fill up and lead to Kaka.

The application architecture becomes complex because it handles communication between multiple processes. Here's another question.

3. Multi-process defects

The memory space between processes is not visible. After you turn on multiple processes, the following issues are raised:

1) repeated reconstruction of the application.

2) Failure of the static member.

3) file sharing issues.

4) breakpoint debugging problem.

4. Workaround

1) Multiple Rebuilds for application:

Get the process ID in application's oncreate to determine the different processes and do different things.

public class MyApplication extends application {

@Override

public void OnCreate () {

Super.oncreate ();

Get process ID

int pid = Android.os.Process.myPid ();

LOG.E ("M_tag", "MyApplication onCreate pid is" + pid); Get the process name based on the process ID

String pName = Getprocessname (this,pid);

if ("Com.xyy.processtest". Equals (PName)) {

The business that processes the process

}

}

}

Public String getprocessname (Context cxt, int pid) {

Activitymanager am = (activitymanager)

Cxt.getsystemservice (Context.activity_service);

list<runningappprocessinfo> Runningapps = am.getrunningappprocesses ();

if (Runningapps = = null) {

return null;

}

for (Runningappprocessinfo Procinfo:runningapps) {

if (procinfo.pid = = pid) {

return procinfo.processname;

}

}

return null;

}

2) Invalidation of static members:

Use process communication such as intent or aidl to pass content, not static or singleton mode.

3) for file sharing issues:

In multi-process situations, there are two processes that access the same database file at the same time. This can result in competitive access to resources, such as database corruption, data loss, and so on. In the case of multi-threaded, we have a lock mechanism to control the sharing of resources, but in the multi-process is difficult, although there are file locks, queues and other mechanisms, but it is difficult to achieve in Android. The solution is that multiple processes do not concurrently access the same file, such as the child process involved in the operation of the database, you can consider invoking the main process for database operations.

4) Debugging Problems for breakpoints:

Debugging is the process of tracking the stack information in the program, because each process has its own separate memory space and its own stack, can not be implemented between different processes debugging. Therefore, to change to the same process: Debug the Androidmanifest.xml in the android:process tag, so that the debug state is in the same process, stack information is consistent. After the debugging is complete, then restore the label.

How to use multi-process, multi-process benefits, multi-process defects, solutions (RPM) in Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.