Process priority in Android

Source: Internet
Author: User

I. Overview

First explain what the process is:
A process is a self-contained program that runs an activity on a data set, which is the basic unit of the system's resource allocation and scheduling, and the process consists of multiple threads running.

In Android, by default, all components within the same application are running in the same process, and most applications do not change it. However, if you need to specify the process to which a particular component belongs, you can use the manifest file to achieve the purpose.

Each component element in the manifest file ——、 、 and--supports the definition of the Android:process property, which specifies the process that the component runs. Setting this property allows each component to run in its own process, or several components to share a process while other components run on separate processes. Setting this property also allows components of different applications to run in the same process-implementing multiple applications that share the same Linux User ID and give the same permissions.

Element also supports the Android:process property, which specifies the default process for all components.

If there is not enough memory, and other processes that provide more emergency services to users require more memory, Android may decide to shut down a process. The application components that are running in this process are also destroyed. When you need to work again, a process is recreated for those components.

When deciding which process to close, the Android system weighs how important they are relative to the user. For example, compared to a process with visible activity, it is more likely to close an activity that is already invisible on the screen. That is, whether to terminate a process depends on the state of the component running in this process

Two. The life cycle of the process

The Android system tries to keep the application process as long as possible, but in order to create new or run more important processes, it is always necessary to purge obsolete processes to reclaim memory. In order to decide which process to keep or terminate, each process is zoned into a hierarchy of importance, depending on the components running within the process and the state of those components. The least important process is first cleared, then the next lowest, and so on, which is necessary to restore system resources.
The importance hierarchy has 5 levels, and the following list lists the various processes by importance (the first type of process is the most important and the last one is terminated):

1. Foreground process
The process that the user must have for the current operation. A process is considered to be in the foreground when either of the following conditions is true:

    • Which runs the activity that is interacting with the user (the Onresume ()] method of the activity object has been called).

    • It runs a service that is bound by the activity being interacted with by the user.

    • Which runs the "foreground" service-the service is called in [Startforeground () mode.

    • It runs a service that is performing a lifecycle callback method (OnCreate (), OnStart (), or OnDestroy ()).

    • Which runs the broadcastreceiver that is executing the onreceive () method.

In general, there are few foreground processes at any moment, only forced--when memory is not enough to sustain them simultaneously--will be terminated. Typically, the device is now at the point of using virtual memory, terminating some foreground processes to ensure a timely response from the user interface.

2. Visible processes
A process that does not have a foreground component, but still affects what the user sees on the screen. A process is considered visible when either of the following conditions is true:

    • It is running activity that is not in the foreground, but the user is still visible to this

    • The activity (OnPause () method is called). This can happen in the following situations: The foreground activity opens a dialog box, and the previous activity is allowed to appear behind.

    • It runs a service that is bound by the visible (or foreground) activity.

The visible process is considered a very important process and will not be terminated unless all foreground processes are not maintained at the same time.

3. Service Process
This process runs a service started by the StartService () method, and it does not upgrade to the above two levels. Although service processes are not directly associated with what the user sees, they typically perform actions that are of interest to the user (such as playing music in the background or downloading data from the network). Therefore, the system keeps the service process running unless the memory is insufficient to maintain all foreground and visible processes running concurrently.

4: Background process
Contains the process of the current user's invisible activity (The OnStop () method of the Activity object has been called). These processes have no direct impact on the user experience, and the system may terminate them at any time to reclaim memory for use by foreground processes, visible processes, and service processes. There are often many background processes running, so they are stored in an LRU (least recently used) list to ensure that the last activity that was recently used by the user is terminated. If an activity correctly implements the lifecycle method and saves the current state, terminating such a process does not have a visible impact on the user experience. Because the activity restores all visible states when the user returns. For more information about save and restore status, see the activity documentation.

5: Empty Process
Processes that do not contain any active application components. The only purpose of preserving this process is to use it as a cache to improve the startup time of the next component running in this process. In order to balance the overall system resources between the process cache and the kernel cache, the system often terminates this process.

Depending on the importance of the current active component in the process, Android evaluates the process to a level that is as high as possible. For example, if a process is running a service and a user-visible activity, the process is evaluated as a visible process, not a service process.

In addition, the level of one process may be enhanced by the dependency of other processes-the process level that provides services to other processes is never lower than the process using the service. For example, if the content provider in a process serves the client in process B, or if the service in process A is called by a component in process B, the a process is considered to be at least as important as process B.

Because the process level for running a service is higher than the background activity process, if the activity needs to start a long-running operation, starting a service for it is better than simply creating a worker thread- This is especially true if the operating time is longer than the lifetime of the activity. For example, an activity that uploads a picture to a Web site should create a service to execute it, even if the user leaves the activity and the upload will continue to run in the background. Regardless of what happens to the activity, using the service guarantees that the operation has at least the priority of the service process. Similarly, the broadcast receiver broadcast receiver uses the service to handle time-consuming tasks rather than simply putting it into the thread.


Process priority in Android

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.