Android中进程生命周期的优先级
“我们不是生产者,我只是大自然的搬运工。”
学习Android最好的途径当然是强大的官方文档了,其中在Processes and Threads一节中对于进程生命周期淘汰优先级,有着详细的介绍。原文如下:
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.
上文大致意思就是说Android系统会尽量维持进程的存在,但毕竟资源有限,当系统资源告急的时候会淘汰一部分进程。淘汰顺序的凭据就是系统进程的优先级了,优先级越高越不容易被杀死,反之亦然。系统总共为进程分了五个优先级,如下(原文后附笔者融合个人理解的简译):
- Foreground process
A process 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 are true:
- It hosts an
Activity
that the user is interacting with (theActivity
'sonResume()
method has been called). - It hosts a
Service
that's bound to the activity that the user is interacting with. - It hosts a
Service
that's running "in the foreground"—the service has calledstartForeground()
. - It hosts a
Service
that's executing one of its lifecycle callbacks (onCreate()
,onStart()
, oronDestroy()
). - It hosts a
BroadcastReceiver
that's executing itsonReceive()
method.
Generally, only a few foreground processes 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.
- It hosts an
- Visible process
A process 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 are true:
- It hosts an
Activity
that is not in the foreground, but is still visible to the user (itsonPause()
method has been called). This might occur, for example, if the foreground activity started a dialog, which allows the previous activity to be seen behind it. - It hosts a
Service
that's bound to a visible (or foreground) activity.
A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.
- It hosts an
- Service process
A process that is running a service that has been started with the
startService()
method and 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 music 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. - Background process
A process holding an activity that's not currently visible to the user (the activity's
onStop()
method has been called). These processes have no direct impact on the user experience, and the system can kill them 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 saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state. See the Activities document for information about saving and restoring state. - Empty process
A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, 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.
一、前台进程(进程满足如下任一条件即为前台进程):
1. 拥有 一个执行了onresume方法正在与用户交互(获得焦点)的Activity
2. 拥有一个service,这个Service跟正在与用户交互的Activity进行了绑定
3. 拥有一个Service,这个Service调用了startForeground()方法
4. 拥有一个正在执行onCreate()、onStart()或者onDestroy()方法中的任意一个的Service
5. 拥有一个正在执行onReceive方法的BroadcastReceiver
二、可见进程:
1. 拥有一个执行了onPause方法,但仍然可见的Activity
2. 拥有一个Service,这个Service跟一个可见的或前台的Activity绑定了
三、服务进程:
拥有一个通过startService方法启动的Service的进程
四、后台进程:
拥有一个后台Activity(onStop方法被调用)的进程
五、空进程:
没有拥有任何活动的应用组件的进程,也就是没有任何Service和Activity在运行
另外,还有一些需要补充的,当一个进程满足多个进程条件时,当然是取优先级更高的为准,比如一个进程同时满足前台进程和服务进程的条件,这个进程就是个前台进程,这点很好理解。另外,进程的优先级也不是一成不变的,而且有时候会随着一些相关的因素而发生改变;比如,某进程A满足前台进程的第二个条件,进程A拥有一个service,这个Service跟正在与用户交互的Activity进行了绑定;当这个Activity变成可见状态了,进程A便不再满足前台进程的条件,进而因满足可见进程的第二个条件,进程A变成了可见进程。总之,在掌握了基本概念之后,需要细心的分析具体的情况,方能得出正确的判断。
Android中进程生命周期的优先级的更多相关文章
- Android 中Service生命周期
使用context.startService() 启动Service 其生命周期为context.startService() ->onCreate()- >onStart()->S ...
- Android 中Activity生命周期分析(二):从AActivity 到BActivity过程分析
如果你没有动手去演示的话,你一定要去动手试试看,这个东西非学容易出错,面试中经常出现,好了,上代码: package com.king.review.base; import android.app. ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- Android中Fragment生命周期和基本用法
1.基本概念 1. Fragment是什么? Fragment是可以让你的app纵享丝滑的设计,如果你的app想在现在基础上性能大幅度提高,并且占用内存降低,同样的界面Activity占用内存比Fra ...
- Android 深入浅出 - 进程生命周期(Process Lifecycle)
Android 5 个进程等级 1. Foreground Process : 2 .Visible Process : 3. Service Process : 4. Background Proc ...
- android中的生命周期(新增2个函数)
onPostOnCreate()和OnPostResme()这两个函数 onPostResume() Called when activity resume is complete (after on ...
- Android 进程生命周期 Process Lifecycle
Android 进程生命周期 Process Lifecycle 进程的生命周期 Android系统会尽力保持应用的进程,但是有时为了给新的进程和更重要的进程回收一些内存空间,它会移除一些旧的进程. ...
- 第11讲- Android中进程及其优先级
第11讲Android中进程及其优先级 进程与线程: 进程:操作系统结构的基础,资源分配的最小单元,一个操作系统包括多个进程: 线程:线程存在于进程当中,是操作系统调试执行的最小单元,一个进程包括多个 ...
- android中进程的优先级
android中进程的优先级
随机推荐
- CSS彻底研究(2)
Github pages 博文 一 . CSS盒模型 1.盒子的结构 margin-border-padding结构 + 内容content 组成盒模型 注意 width,height 取的是cont ...
- .net中用到的一些方法
//文件操作string fullDirPath = Utils.GetMapPath(string.Format("/aspx/{0}/", buildPath)); Direc ...
- table-cell完成左侧定宽,右侧定宽及左右定宽等布局
使用table-cell完成以下几种布局(ie8及以上兼容) 1.左侧定宽-右侧自适应 .left{ width: 300px; height: 500px; border: 1px solid; f ...
- C#实现防拷贝工具示例
思路是用加密程序 对硬盘号,cpu号和MAC号取出字符串并加密 产生一个序列号 每次程序启动后重新产生这个序列号并比对,如果一致则验证通过 using System;using System.Coll ...
- MongoDb笔记(一)
1.Mongodb 数据库是动态生成的可以使用use 数据库名 来指定要使用的数据库,如果数据库不存在就自动生成一个 2.插入一个文档:db.foo.insert({"name": ...
- 关于 knockout js 学习中的疑问 (1)
最近刚刚学习knockout中遇到如下问题: 1.在给viewModel定义一个方法时,有时后面跟 的this,有的时候没有 如下所示: this.fullName = ko.computed(fun ...
- 关于iOS9之后的loadViewIfNeeded
iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...
- poj 3959 Alignment of Code <vector>“字符串”
Description You are working in a team that writes Incredibly Customizable Programming Codewriter (IC ...
- python-整理--pip whl命令
如果要在windows系统上安装新的包,可以下载*.exe安装文件,双击下一步...,如果找不到exe的话. 在CMD中执行 pip install 安装包文件.whl 就可以安装了 pip这个命令本 ...
- php 查看文档
http://www.runoob.com/php/php-datatypes.html php 学习网站 : http://www.phpfans.net/