Android knowledge about the process, you need to know (a)

Understand the concept of process

A process is executing a program, the basic unit of resource allocation and scheduling system, with its own dedicated memory resources.

Process is the concept of the operating system, so in terms of the PC or mobile end are there, the process on the PC side, the performance of programs, such as music players, chat software, etc.; on the phone, is an applications software.

The difference between processes and threads

Look at a map

From the above picture, we can see, the process that contains the thread . A process where you can have multiple threads of such a relationship.

Each process has a part of his memory allocated by the operating system, by default, the memory area A process and other processes can not share. Just use the word describing a default situation. Speaking of data can not be shared between processes, this is not absolute. In special cases also can be shared, this is the IPC mechanism. IPC mechanism full name is called: inter-process communication, that is, inter-process communication . About IPC mechanism, currently we know like, this article does not discuss in detail IPC mechanism to follow-up a special mechanism to write articles about the IPC Android system.

Threads do not like the process, the process can not be shared memory, and threads can. A thread is included in the process. The memory is allocated by a thread over the corresponding process from that. Because of its easy to create threads, shared memory, and other characteristics suitable for concurrency scenarios, often referred to as lightweight processes . Because data can be shared between threads, opened in concurrent programming so often there will be a careless resulting concurrency issues, the problem is not synchronized between multiple threads of data caused. This is the multi-threaded development should pay attention to.

Setting process

In the Android system, by default, APP is a single development process. In general, the business needs on multiple processes is relatively small, so a single process quite enough. If you need multiple processes, then, Android also provides a way to implement multi-process. The method of the Android Manifest file is set to four components android android: process attribute. By default, all running in the same process, the default process name of the application package name.

for example:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SecondActivity"
            android:process=":second"
            >
        </activity>
        
        <activity android:name=".ThirdActivity"
            android:process="com.example.rainyang.demo.third"
            />
复制代码

MainActivity is running in the default process, a process called application package name: com.example.rainyang.demo;

SecondActivity process running named: com.example.rainyang.demo: second ,: is a shorthand to indicate before the current process name with the package name;

Process ThirdActivity run named: com.example.rainyang.demo.third.

Examples of the above-cited Activity is represented by one of the four components Similarly, process use the same attribute, the same effect Service, ContentProvider, Broadcast medium.

The process is also a priority of the division, you think, ah, the phone's memory is so big, when running multiple processes (app), the system ran out of memory, the process requires memory currently in use, how to do? Always have to memory, so users can not use it. The system will be selected according to priority of a process to kill lowest priority process, its memory is reclaimed, then put the memory allocated to the process the user is currently interacting with. Thus ensuring the normal use of the user.

Next we see the classification of the process.

Process category

The process of classification is based on the priority level of the process as the standard, it was classified. The following descending order of priority from the various processes are introduced.

foreground process (foreground process)

Foreground process is that the user is interacting with the process is necessary for the operation process is currently being executed. Meet any of the conditions below, the process in which they all belong to the foreground process.

  • The user is interacting components and Activity, and Activity of onResume callback is being executed. Process Activity component belongs to foreground process.
  • onReceived BroadcastReceiver component () method is executing. In the process it is foreground process.
  • onCreate Service service () method, onStart () method, onDestroy () these three methods, if there is a method being executed. At the moment, it belongs to the process for the foreground process.

Foreground process is the highest priority, generally speaking, the system does not kill it, reclaim its memory. You think, ah, foreground process is the process of operating the user is using, the system suddenly recovered, and that the user experience much worse. For foreground process it is generally a process system for its services, officially because the foreground process requires memory, the system will go to kill other processes, recovery of memory, to meet it.

Through the above meet several conditions foreground process can be found, and these cases are the four Android components are related. Look at the way when it comes to set up multi-process before, in the four major components of the process set by the property. These are echoes. To some extent, it can be understood as, on behalf of the four components of the APP and implement the process.

Visible process (visible processes)

Visible process is doing something of interest to users, although its priority is lower than the foreground process priority, but the word from its description - "customer care" can be seen, it can not easily kill it, or user of psychological affirmation accurate. When either of the following conditions is satisfied, it represents a visible process in the process:

  • Activity visible, but not (in the Activity onPause callback is called) in the foreground and user interaction, how to understand it? You think about a situation, appear above a pop Activity, Activity at the moment is visible, but the current user's attention is the interaction point pop.
  • () Method by calling startForeground Service, opening a front desk.
  • Service is a host system for a particular function of the user's attention, such as dynamic wallpaper, input services.

This process, the priority is relatively high, generally not easy to kill.

Service process (service process)

When the Service component calls startService () method to enable a service that belongs to the process as a service process. Although the service process will not be as foreground process, the process can be seen as directly visible to the user, but often with some of the things users care about, such as uploading and downloading data in a logical background network service process. So unless memory is not enough, and generally do not go on to kill the service process in the case of memory sufficient.

Has been running for a long time (such as 30 minutes or more) of the Service may be downgraded to allow its process to drop the cache LRU list described below. This helps to avoid memory leaks or other problems in the long run serve up a lot of RAM and cause the system can not effectively use the cache process.

Cached process (process cache)

Cache process is the lowest process priority when needed elsewhere memory, the system will give priority to kill the process. (Poor thing ..), killing of such a process will not affect the user experience.

In general, the system will have multiple cache process aimed at switching between applications play a more effective role switching (cache Well, the spare tire both visual sense), and according to the system needs regular cleaning oldest cache process. System after all the processes are killed reclaim memory cache, memory does not meet the need, will choose to start service process.

summary

On the face of several types of understanding of the process, developers can according to the needs, the logic in the right process, to avoid significant business logic into a low-priority process, resulting in service being killed, the user experience is not well, this situation.

IPC

Between IPC (inter-process communication) process communication, there is the beginning of the article mentioned. The mechanism of action is to meet the needs of communication between multiple processes. Android system, multi-process mode of communication has Binder, AIDL (also internal Binder), Socket, ContentProvider and so on. About IPC mechanisms to achieve, explain in detail in a later article.


Writing is not easy, I feel pretty good, then it is the point of a " Like " to encourage the next bar!



Reproduced in: https: //juejin.im/post/5cfcfe98e51d4550bf1ae82f

Guess you like

Origin blog.csdn.net/weixin_33966095/article/details/91466777