The life cycle of the Android process

Source: Internet
Author: User

It is almost impossible for an Android system to keep an application process permanently, so the system needs to constantly release old or less important processes to free up enough memory to run new or more important processes, so how does the system decide which process should keep what should be killed? Originally the system will be based on the running components in the process and their status to list an "important level", when the memory tension, the system will first kill the least important process, followed by the second, the third ... , and so on, to constantly release system resources until the memory is no longer tense.

The process is divided into 5 levels according to this important hierarchy, and the following are five different types of processes:

1. Foreground process

This process type needs to know what the user is currently doing and can be considered a foreground process if any of the conditions are met:

(1) In Activity resume state
(2) Being in the State of bound service interaction
(3) The state in which the service is running in the foreground (Startforeground () is called)
(4) Service life cycle is being executed (onCreate (), OnStart (), OnDestroy ())
(5) Broadcastreceiver executing onreceive () method

Killing foreground requires a user response, because this security priority is the highest that is required by the user, and at any given time, only a handful of processes will be in the foreground, only to be killed if the memory is simply not available for them to run concurrently. In general, in this case, the device is still in the state of using virtual memory, and some foreground processes must be killed to remain responsive in the user interface.

Android will measure the level of a process as high as possible based on the importance of the current active component in the process. For example, if a process has both a service and a visual activity, the process is judged to be a visual process, not a service process.

2. Visible process

(1) activity is not displayed at the front, but it is not completely hidden, can be seen, such as pop up a dialog box
(2) A service that bound to the visible or foreground activity

There is no foreground component, but it can still be seen by the user on the screen. A process is considered visible when either of the following conditions is true:

? It contains an activity that is not in the foreground but is still visible to the user (its OnPause () method is called). This situation may occur if, for example, the foreground activity is a dialog box, and the previous activity is below it and can be seen.
? It contains a service that is bound to a visual activity.
Visual processes are still considered important and cannot be killed until the foreground process is run without killing them.

3. Service Process

A service that is running, not in either of the above states. is a service started by the StartService () method, which does not become the above two classes. Although service processes are not directly visible to the user, they are generally doing what the user cares about (such as playing MP3 in the background or downloading things from the Internet). As a result, the system tries to keep them running, unless the system is running out of memory to maintain the foreground and visual processes.

4. Background process

Invisible State of the activity process, OnStop is called.

Contains activity that is not currently visible to the user (the OnStop () method of the Activity object has been called). These processes are not directly linked to the user experience and can be killed at any time to reclaim memory for use by foreground processes, visual processes, and service processes. In general, there are many background processes running, so they are generally stored in an LRU (last-use) list to ensure that the last activity that is used by the user is finally killed. If an activity correctly implements the lifecycle method and captures the correct state, the process that kills it has no adverse effect on the user experience.

5. Empty process

There are no component processes running, this process is reserved primarily for caching needs

Does not contain any active application components. The only reason this process exists is to cache to improve the startup time when the component is running again. This process is often killed by the system to maintain a balance between the process cache and the system kernel cache.


In addition, the level of a process may be elevated because other processes depend on it. A process level that provides services to other processes is always higher than the process that uses it. For example, if a content provider in a process serves a client in process B, or if the service in process A is bound to a component in process B, the a process is considered to have the same importance as process B.

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.