The Android system will continue the application process as long as possible, but it will inevitably need to remove the old process when the memory is too low. To decide to keep or remove a process, Android puts each process into a "level of importance", based on the component it is running on and its state. The least important process is first eliminated, then the lower, and so on. The importance is divided into five layers, according to the importance list as follows:
1. Foreground process
• It is necessary for the user to operate, at any one time, only a few processes will be in the foreground and will only be killed if the memory is 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. Visual process
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
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
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
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.
Android process and life cycle introduction