android-Multi-process first knowledge

Source: Internet
Author: User

android-Multi-process initial knowledge learning from

Explore the art of Android development
Https://baike.baidu.com/item/%E8%BF%9B%E7%A8%8B/382503?fr=aladdin#1
56278433

Processes and Threads

IPC (inter-process communication) refers to interprocess communication, which refers to the process of exchanging data between two processes. Before we learn the IPC we need to know what the process is and what the thread is.

A process is an instance of an application that is the smallest unit of resource allocation and scheduling for an operating system, each of which represents an instance of an application

A thread is the smallest unit that a program executes, and the thread itself is not a resource (except for the resources that maintain itself), and the thread is contributing resources to the process.

A process includes at least one thread (the UI thread), but if you perform a lot of time-consuming operations in the UI thread, it will cause the UI to be unresponsive. Of course, this is not advisable.

Benefits of using multiple processes

Although the use of multi-process later in the data communication is more complex and may encounter a variety of problems, but multi-process also has its own benefits. It is well known that there is a limit to the memory that every Android application can invoke, but if the allocated memory is not enough for our application, then we can get more memory resources in a multi-process way.

And if our application needs a few separate modules, it needs to take a multi-process.

Dalvik virtual Machines

The Android system has a different memory space for each process that is assigned a separate Dalvik virtual machine. objects that access the same class between different processes are created differently 副本 . These replicas are independent of each other and do not interfere with each other. This is why it is obviously not successful if we want to share data in a multi-process mode between two different processes in memory. IPC technology is necessary if you want to communicate between processes.

Open Multi-process
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" TOP.LITTLEDAVID.STUDYIPC "> <application android:allowbackup=" true "Android:ico        n= "@mipmap/ic_launcher" android:label= "@string/app_name" android:roundicon= "@mipmap/ic_launcher_round" Android:supportsrtl= "true" android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&        Gt <activity android:name= ". Secondactivity "android:process=": Remote "/> <activity android:name=".  Thirdactivity "android:process=" Top.littledavid.studyipc.remote "/> </application></manifest>

The four components can be run in different processes, with process properties specifying the manifest process for the four components in the file.
Invoke activity

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        this.startActivity(android.content.Intent(this, SecondActivity::class.java))    }}class SecondActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_second)        startActivity(Intent(this, ThirdActivity::class.java))    }}

In the above three activity mainactivity did not specify the process, then mainactivity is the default process, the default process is named the program 包名 , if you want to modify the default process, Application specify the process through the properties of the node process . The process of secondactivity is that the process of top.littledavid.studyipc:remote thirdactivity is top.littledavid.studyipc.remote that when different activity is run, different processes are created. You can view processes that are already running by using ADB naming or DDMS.

adb shell ps ##查看所有进程### 下面的进程是我们程序的进程u0_a84    4022  1377  1416356 49952 SyS_epoll_ 00000000 S top.littledavid.studyipcu0_a84    4039  1377  1415804 49424 SyS_epoll_ 00000000 S top.littledavid.studyipc:remoteu0_a84    4056  1377  1418396 50172 SyS_epoll_ 00000000 S top.littledavid.studyipc.remote

This is the process we started. top.littledavid.studyipcThis is our default process, while the remaining two make our newly created process. In addition to the default process, the remaining two processes are not the same at the time of declaration, one is :remote <包名>.remote . :There are two of these effects

    1. The most obvious one: omit the package name, is a shorthand way??
    2. Indicates that the current process is a private process, and that UID the process is shared across multiple apps
Shared processes

In the above we mentioned the concept of shared processes, it is well known that Android is based on Linux system, Android system in the permission settings have a feature:

    • Android is a multi-user Linux system, each Android application is a different user
    • By default, each app is assigned a unique Linux user ID, and the system sets permissions for the file, and only that user can access
    • Each process has its own virtual machine, so applications and applications are isolated from each other.

This multi-user approach restricts each app from accessing only the components that are relevant to itself, not the unrelated components, and by arranging two apps to share a Linux user ID, in which case they can access each other's file components.

Verify that there is no shared memory space between processes

Two activity MainActivity and SecondActivity , secondactivity in another process. Validate our theory by modifying a static variable.

//将会在两个Activity中修改i变量,来验证object ValueHolder {    public var i = 0}class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        ValueHolder.i = 1        this.startActivity(android.content.Intent(this, SecondActivity::class.java))    }}class SecondActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_second)        Log.e("TAG", ValueHolder.i.toString())        //输出 0    }}

In the code above, we might think that it should be output 1 , but don't forget that secondactivity is in another process, that is, in two different virtual machines, when the same object is accessed in different virtual machines, a different copy is generated, and the replicas do not affect each other. This confirms the theory above us.

Impact of multi-process mode

Because different processes run with different virtual machines causing memory to not be shared, the following effects occur:

    1. Static members and Singleton modes are completely invalidated
    2. Thread synchronization is completely invalidated
    3. Sharedpreference Reliability Down
    4. Application objects are created multiple times

The cause of ① and ② is the same because it's not the same block of memory, so the thread's sync lock doesn't work.

③ is because the bottom of sharedpreference is to read and write XML files, while concurrently writing to the same file may result in data loss.

④ know that the application class represents an instance of an application, so the reason is simple because the system will create a virtual machine for each process, which is also the process of creating an instance of the application and starting it, so that's why application is created multiple times.

Summarize

In this chapter, we briefly understand the concept of multi-process and the impact of multi-process, in the next chapter, we will learn the basis of IPC.

android-Multi-process first knowledge

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.