Android process and thread details 1: Process

Source: Internet
Author: User

When an application component starts running and no other components are running, the system starts a new Linux Process for the application, which has only one thread. by default, all components of an application run in one process and thread (main thread. if the thread of an application starts to run and the application thread already exists (because another component of the application is already running ), the component is started in the existing process and runs in the same thread. however, you can schedule different components to run in different processes, and you can create additional threads for any program.

Process

By default, all components of the same program run in the same process and most programs do not have to change this situation. However, if you want to be different, you can also modify the manifest file.

All items in the manifest file that support the Android: process attribute (<activity>, <service>,
<Handler ER>, and <provider>) can specify a process, so these components will run in this process. you can set this attribute so that each component runs in its own process or only some of the components share a process. you can also set Android: process so that components of different applications can run in the same process-if these applications share the same user ID and have the same digital certificate.

The <Application> element also supports the Android: process attribute, which is used to specify a default value for all components.

Android may decide to shut down a process at some time, for example, when the memory is small and another process needs to be started more urgently. when a process is disabled, all components are destroyed. if you re-require these components to work, the process will be created again.

When deciding which threads to close, the android system will measure the closeness between processes and users. for example, processes with all invisible activities are more likely to be closed than processes with visible activities. how to determine whether a process is shut down depends on the status of components running in the process. the rules for shutting down processes will be discussed below.

Process Life Cycle

The Android system tries its best to maintain the life of a process until it needs to free up memory for a new and more important process. In order to decide which one should be killed and which one should be left, the system will place the process in different importance levels based on the component and component Status running in the process. When system resources are required, the lower the importance level, the first elimination.

Importance levels are divided into five levels. The importance levels of different types of processes are listed below (the first process type is the most important and will be killed at the end ):

1 foreground Process

This process is required for what the user is currently doing. If the following conditions are met, a process is considered as a foreground process:

This process has an active activity (onresume () of this activity ()
Method ).

This process has a service bound to the activity that is interacting with the user.

This process has a front-end running service-service that calls the method.
Startforeground ().

This process has a method (oncreate (), onstart (),
Or ondestroy () service.

This process has a broadcastreceiver that is executing its onreceive () method.

Generally, at any point in time, only a few foreground processes exist. They will be killed only when they reach a conflict that cannot be adjusted-if the memory is too small to continue running. Usually, the device reaches a memory paging scheduling state, so some foreground processes need to be killed to ensure the user interface response.

2 visible process

A process does not have components running on the foreground, but it still affects what you see. When the following conditions are met, the process is visible:

This process has an activity that is not in the foreground but is still visible (its onpause () method is called ). This happens when a foreground activity starts a dialog box.

3. Service Process

A visible process is considered extremely important. In addition, all foreground processes cannot be operated unless it is killed.

This process has a service bound to a visible activity.

A process is not in either of the preceding two types, but it runs a service started by startservice.

Although a service process does not directly affect what users see, it usually does things that users care about (such as playing music or downloading data ), therefore, the system will not kill the foreground and visible processes if they cannot survive.

4 background processes

A process has an currently invisible activity (the onstop () method of the activity is called ).

Such processes do not directly affect the user experience, so the system can kill them at any time to provide storage space for foreground, visible, and service processes. Many background processes are usually running. They are saved in an LRU (least recently used) list to ensure that the process with the activity that was recently viewed is finally killed. If an activity correctly implements its lifecycle method and saves its current state, the process that kills it will not affect the user's visual experience. Because when the user returns to this activity, this activity will restore all its visible states.

5. Empty Process

A process does not have any active components.

The only reason for retaining such processes is high-speed cache, which can increase the startup speed of the next component to run it. The system often kills the entire system resource to balance the process cache and the underlying kernel cache.


The importance of the currently active components in the process,AndroidThe process will be arranged at the highest possible level. For example, if a process hasServiceAnd a visibleActivityThe process is identified as a visible process rather than a service process.

In addition, if other processes are dependent, the level of a process may be increased-a process serving other processes cannot be at a lower level than the process being served.

Because you haveServiceProcesses with a backgroundActivitieThe process level is high, so whenActivityWhen starting an operation that requires long execution, it is best to start a service instead of simply creating a working thread. Especially when this operation may beActivityIt takes longer to live. For example,Activity, You should startServiceSo that the upload operation can be performed when the user leaves thisActivityIn the background. Use oneServiceEnsure that this operation is at least"Service Process"Level.ActivityWhether there is any misfortune. This should also be used by the broadcast ReceiverServiceInstead of simply using a thread.


Related Article

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.