Application Fundamentals--Processes and lifecycles(进程生命周期)

Processes and lifecycles---进程生命周期

   The Android system tries to maintain an application process for as long as possible, but eventually it will need to remove old processes when memory runs low. To determine which processes to keep and which to kill, Android places each process into an "importance hierarchy" based on the components running in it and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest, and so on. There are five levels in the hierarchy. The following list presents them in order of importance:

翻译:Android 系统通常总是尽可能延长应用程序所属进程的生命期的,但是,系统最终还是会因为系统资源问题要关闭进程的。关闭进程之前,系统需要判定哪些进程需要继续维持哪些可以被关闭,Android系统是根据每个进程上关联的组件实例以及组件实例的最新状态做为判定每个进程重要程度的依据的,最低重要程度的进程将首先被系统关闭, 重要程度共分五个等级(重要程度1-5逐次降低,重要程度在这里也可以理解成系统维护进程等级):

   1. A foreground process is one that is required for what the user is currently doing. A process is considered to be in the foreground if any of the following conditions hold:---翻译:符合下列四个条件的进程属于 前端进程(foreground process):

          * It is running an activity that the user is interacting with (the Activity object's onResume() method has been called).--翻译:进程中存在某个activity实例,该实例处于和用户交互状态。(该实例的onResume()方法已经被系统调用)。

          * It hosts a service that's bound to the activity that the user is interacting with.--翻译:进程中某个服务被绑定到某个正在与用户交互的activity实例上。

          * It has a Service object that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()).
            --翻译:进程中某个Service实例的回调方法:onCreate(), onStart(), 或 onDestroy()正在被系统执行。

          * It has a BroadcastReceiver object that's executing its onReceive() method.---翻译:进程中某个BroadcastReceiver实例的onReceive()方法正在执行中(广播消息响应处理中)。

      Only a few foreground processes will exist at any given time. They are killed only as a last resort — if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.

      翻译:由于系统同时只能维护少数几个前端进程,系统关闭前端进程是最不得以的做法,比如内存不足以维持前端进程的运行的时候才会发生。另外,为了保证与用户快速交互,某些前端进程也会被系统关闭的。

   2.A visible process is one that doesn't have any foreground components, but still can affect what the user sees on screen. A process is considered to be visible if either of the following conditions holds:

      翻译:可视进程中没有任何组件处于前台与用户交互,但是在屏幕上对用户视觉还是影响,符合下面条件的进程属于 可视进程:

          * It hosts an activity that is not in the foreground, but is still visible to the user (its onPause() method has been called). This may occur, for example, if the foreground activity is a dialog that allows the previous activity to be seen behind it.---翻译:进程中某个activity组件实例已经不在前台,但是对于用户而言依然是可见状态(该实例的 onPause() 方法已经被系统调用过),例如,当前处于前台与客户交互的是一个对话框,那么前一个activity界面作为背景依然是用户可见状态。

          * It hosts a service that's bound to a visible activity.---翻译:进程中某个服务实例与某个可视状态的activity有着绑定关系。

      A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.---翻译: 可视进程是非常重要的,除非系统需要维护所有的前端进程,否则可视进程是不会被系统关闭的。

   3. A service process is one that is running a service that has been started with the startService() method and that does not fall into either of the two higher categories. Although service processes are not directly tied to anything the user sees, they are generally doing things that the user cares about (such as playing an mp3 in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.

      翻译: 服务进程 ,是正在运行某个由 startService() 方法启动的服务的进程,尽管说 服务进程并没有直接与用户发生交互,但是它所做的事是用户关注的事,比如在后台播放MP3音乐或是在后台通过网络下载数据,所以系统通常总是维持这样的进程的,除非前端进程、可视进程需要更多内存的时候。可以这样理解前端进程、 可视进程的重要程度要高于 服务进程

   4. A background process is one holding an activity that's not currently visible to the user (the Activity object's onStop() method has been called). These processes have no direct impact on the user experience, and can be killed at any time to reclaim memory for a foreground, visible, or service process. Usually there are many background processes running, so they are kept in an LRU (least recently used) list to ensure that the process with the activity that was most recently seen by the user is the last to be killed. If an activity implements its lifecycle methods correctly, and captures its current state, killing its process will not have a deleterious effect on the user experience.

      翻译: 后台进程 是持有某个当前用户不可见的activity实例的进程,(被持有的Activity实例的 onStop() 方法已经被系统调用过). 后台进程就用户体验来说没有什么影响,一旦 前端进程可视进程服务进程需要更多内存资源,系统将会随时杀死 后台进程。通常有很多 后台进程会处于系统维护中状态,系统把所有这些后台进程放在一个 LRU (least recently used) list中,这样做的目的是保证最后被用户看到的activity所属的进程总是最后一个被系统杀死。只要一个activity的回调方法被开发者正确实现、并能够及时俘捉到自身最新状态信息,那么,该实例所属的进程即使被系统关闭也不会影响到客户体验的。

   5.An empty process is one that doesn't hold any active application components. The only reason to keep such a process around is as a cache to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

      翻译: 空进程是不持有应用的任何组件实例的进程。系统维护这样的进程的目的是处于缓存的考虑,可以提高组件实例的启动效率。系统也常常杀死这样的进程以换取系统资源、进程缓存、底层核心缓存之间的平衡。

   Android ranks a process at the highest level it can, based upon the importance of the components currently active in the process. For example, if a process hosts a service and a visible activity, the process will be ranked as a visible process, not a service process.

翻译:Android系统尽可能授予进程更高的维护级别,具体说是根据当前进程中关联的activity实例的状态来给定当前进程维护级别,例如一个进程中关联到一个服务和一个可视的activity实例,该进程将被给定为 可视级别。

   In addition, a process's ranking may be increased because other processes are dependent on it. A process that is serving another process can never be ranked lower than the process it is serving. For example, if a content provider in process A is serving a client in process B, or if a service in process A is bound to a component in process B, process A will always be considered at least as important as process B.

翻译:另外,一个进程的系统维护级别可能会由于被其他进程依赖而提高,被依赖的进程的级别总是高于依赖进程。例如:进程A中的内容提供组件是为进程B的一个组件服务的,或是如果进程A中的一个服务实例被绑定到进程B的某个组件实例,那么进程A至少具有和进程D同样的系统维护级别。

   Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. Examples of this are playing music in the background and uploading a picture taken by the camera to a web site. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. As noted in the Broadcast receiver lifecycle section earlier, this is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.

翻译:由于 服务进程(执行某个服务的进程)的系统维护级别总是高于某个关联了一个activity实例的 后台进程的系统维护级别,这就意味这 服务进程要比这个关联了一个activity实例的 后台进程活的更长。所以,使用 服务执行长时间操作要比使用一个activity实例来执行同样的耗时操作要更为合适。例如:后台音乐播放和把用照相功能俘获的图像信息上次到某个网站这样 的耗时操作,如果使用的是服务组件,那么当前进程就是 服务进程维护级别。在Broadcast receiver lifecycle章节中,也说到过类似的问题,都是出于同样的考虑,广播接收组件实例的onReceive()方法中不应该用独立线程的方式执行耗时操作,而是应该使用服务组件执行这样的操作。

你可能感兴趣的:(thread,android,cache,网络应用,音乐)