一 . android进程的优先级
Foreground process
Visible process
Service process
Background process
Empty process
系统会主动收回4,5号进程.
二 .android 进程的回收策略
- Low memory killer: 通过一些比较复杂的评分机制,对进程进行打分,然后将分数高的进程判定为bad进程,杀死并释放内存.
OOM_ODJ:判别进程的优先级.
Low memory killer会根据OOM_ODJ这个值来判定kill那些进程.
三 .进程保活方案.
- 利用系统广播拉活.
利用系统Service机制拉活: 利用onStartCommand方法返回START_STICKY,此拉活是针对Low memory kill的情况下,如果开始service就被杀死会在5s内系统重启service,如果第二次被杀死那么系统将在10s重启service, 如果第三次被杀死那么系统将在20s重启service. 三次后就无法了哦。
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart\(intent, startId\); return START_STICKY;}
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then leave it in the started state but
* don't retain this delivered intent. Later the system will try to
* re-create the service. Because it is in the started state, it will
* guarantee to call {@link #onStartCommand} after creating the new
* service instance; if there are not any pending start commands to be
* delivered to the service, it will be called with a null intent
* object, so you must take care to check for this.
*
* <p>This mode makes sense for things that will be explicitly started
* and stopped to run for arbitrary periods of time, such as a service
* performing background music playback.
*/
public static final int START_STICKY = 1;
利用Native进程拉活:android5.0后就无法使用了,原来是用native service来监控AMS.
利用JobScheduler机制拉活:监听主进程的存活,来决定是否重启service.
利用账号同步机制拉活