Android开发中进程等级相关

来源:互联网 发布:大数据 选股 编辑:程序博客网 时间:2024/05/17 09:46
 Android五个进程等级

  1、前台进程(Foreground process):
  用户当前工作所需要的。一个进程如果满足下列任何条件被认为是前台进程:

  正运行着一个正在与用户交互的活动(Activity对象的onResume()方法已经被调用)。
  寄宿了一个服务,该服务与一个与用户交互的活动绑定。
  有一个Service对象执行它的生命周期回调(onCreate()、onStart()、onDestroy())。
  有一个BroadcastReceiver对象执行他的onReceive()方法。

  在给定时间内仅有少数的前台进程存在。仅作为最后采取的措施他们才会被杀掉——如果内存太低以至于他们不能继续运行。

  2、可视进程(Visible process):
  没有任何前台组件,但是仍然能影响用户在屏幕上看到东西。一个进程满足下面任何一个条件都被认为是可视的:

  寄宿着一个不是前台的活动,但是它对用户仍可见(它的onPause()方法已经被调用)。举例来说,这可能发生在,如果一个前台活动在一个对话框(其他进程的)运行之后仍然是可视的,比如输入法的弹出时。
  寄宿着一个服务,该服务绑定到一个可视的活动。

  一个可视进程被认为是及其重要的且不会被杀死,除非为了保持前台进程运行。

  3、服务进程(Service process):是一个运行着一个用startService()方法启动的服务,并且该服务并没有落入上面2种分类。虽然服务进程没有直接关系到任何用户可见的,它们通常做用户关心的事(诸如在后台播放mp3或者从网络上下载数据),因此系统保持它们运行,除非没有足够内存来保证所有的前台进程和可视进程。

  4、后台进程(Background process):是一个保持着一个当前对用户不可视的活动(已经调用Activity对象的onStop()方法)(如果还有除了UI线程外其他线程在运行话,不受影响)。这些进程没有直接影响用户体验,并且可以在任何时候被杀以收回内存用于一个前台、可视、服务进程。一般地有很多后台进程运行着,因此它们保持在一个LRU(least recently used,即最近最少使用,如果您学过操作系统的话会觉得它很熟悉,跟内存的页面置换算法LRU一样。)列表以确保最近使用最多的活动的进程最后被杀。

  5、空进程(Empty process):是一个没有保持活跃的应用程序组件的进程。保持这个进程可用的唯一原因是作为一个cache以提高下次启动组件的速度。系统进程杀死这些进程,以在进程cache和潜在的内核cache之间平衡整个系统资源。

  一个进程的排名因为其他进程依赖它而上升。一个进程服务其它进程,它的排名从不会比它服务的进程低。例如,进程A中的一个内容提供者服务进程B中的一个客户,或者进程A中的一个服务绑定到进程B中的一个组件,进程A总是被认为比进程B重要。

  因为一个服务进程排名比后台活动的进程排名高,一个活动启动一个服务来初始化一个长时间运行操作,而不是简单地衍生一个线程——特别是如果操作很可能会拖垮活动(例如出现ANR)。这方面的例子是在后台播放音乐和上传相机拍摄的图片到一个网站。使用服务保证操作至少有“服务进程”的优先级,无论活动发生什么情况。

 

附google SDK description:

Processes

By default, all components of the same application run in the same process and most applications should not change this. However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.

The manifest entry for each type of component element—<activity>,<service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

The <application> element also supports an android:process attribute, to set a default value that applies to all components.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.

When deciding which processes to kill, the Android system weighs their relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. The rules used to decide which processes to terminate is discussed below.

Process lifecycle

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.

There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process ismost important and is killed last):

1

2

3

4

5

 

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 is ranked as a visible process, not a service process.

In addition, a process's ranking might 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 is always considered at least as important as process B.

Because a process running a service is ranked higher than a process with background activities, an activity that initiates a long-running operation might do well to start aservice for that operation, rather than simply create a worker thread—particularly if the operation will likely outlast the activity. For example, an activity that's uploading a picture to a web site should start a service to perform the upload so that the upload can continue in the background even if the user leaves the activity. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. This is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.

 

原创粉丝点击