Android进程保活总结

关于Android进程保证存活方法总结
摘要由CSDN通过智能技术生成

最近学习了关于Android进程保活的相关知识,在此写下一篇博客总结下。

Android 进程优先级

如果需要保证我们的应用所在进程存活,那么我们就应该先了解一下Android系统中进程的分类。

在Android中,进程依据重要性被分为5级,越高级的进程越重要,在内存不够回收进程时也会越晚被回收:

前台进程(Foreground process):指用户当前操作必须的进程。一般来说,系统中仅存在极少的前台进程,而且它们会到最后才被回收掉。拥有以下特征的进程被视为前台进程:

  • 拥有Activity运行在屏幕最前端的进程。(已调用Activity的onResume方法)
  • 拥有正在运行的BroadcastReceiver的进程。(已调用BroadcastReceiver的onReceive方法)
  • 拥有正在执行的Service的回调方法的进程。(Service的onCreate、onStart、onDestroy方法)

可见进程(Visible process):正在执行某些用户可见操作的进程。当杀死这些进程时,用户会有一定的影响。拥有以下特征的进程被视为可见进程:

  • 拥有可视的,但不是在前台的Activity。(已调用onPause方法)(当另一个个Activity设置为dialog模式时可能出现这种情况)
  • 拥有一个前台Service。(通过用startForeground)
  • 拥有系统关键特性的Service,如动态壁纸、输入法等服务。

服务进程(Service process):拥有一个正在运行的Service的进程。通常这种进程都是不可见的,会在运行较长的时候后考虑降级回收掉。

后台进程(Background process):这是一种对用户体验没有直接影响的进程,系统会在需要内存的时候随时回收这种进程,这种进程通常会持有一个已调用onStop方法的Activity。

空进程(Empty process):不含任何活动应用组件的进程。保留这种进程的的唯一目的是用作缓存,以缩短下次在其中运行组件所需的启动时间。 为使总体系统资源在进程缓存和底层内核缓存之间保持平衡,系统往往会终止这些进程。



更多详细的进程优先级内容可以参考Android官方文档:

https://developer.android.com/guide/topics/processes/process-lifecycle.html

https://developer.android.com/guide/components/processes-and-threads.html


Android 进程回收策略

众所周知,Android是基于Linux系统的。在Android进程回收策略中,Android进程与Linux进程根据OOM_ADJ阈值进行区分:

  • OOM_ADJ >= 4:比较容易被杀死的进程
  • OOM_ADJ 0 ~ 3:不容易被杀死的进程
  • OOM_ADJ < 0 :纯Linux进程,非Android进程
当Android系统察觉设备内存不足时,会按照阈值从大到小杀死进程。

具体的oom_adj值的意义我们可以查看AOSP中的com.android.server.am.ProcessList 文件(其中本人添加了一些中文注释):
/**
 * Activity manager code dealing with processes.
 */
final class ProcessList {
	...

    // OOM adjustments for processes in various states:

    // Adjustment used in certain places where we don't know it yet.
    // (Generally this is something that is going to be cached, but we
    // don't know the exact value in the cached range to assign yet.)
	// 未知进程,通常是用作缓存
    static final int UNKNOWN_ADJ = 16;

    // This is a process only hosting activities that are not visible,
    // so it can be killed without any disruption.
	// 拥有不可视的Activity的进程,可以不影响影响用户的情况下杀掉
    static final int CACHED_APP_MAX_ADJ = 15;
    static final int CACHED_APP_MIN_ADJ = 9;

    // The B list of SERVICE_ADJ -- these are the old and decrepit
    // services that aren't as shiny and interesting as the ones in the A list.
	// 一些旧的服务进程
    static final int SERVICE_B_ADJ = 8;

    // This is the process of the previous application that the user was in.
    // This process is kept above other things, because it is very common to
    // switch back to the previous app.  This is important both for recent
    // task switch (toggling between the two top recent apps) as well as normal
    // UI flow such as clicking on a URI in the e-mail app to view in the browser,
    // and then pressing back to return to e-mail.
	// 用户使用的前一个进程
    static final int PREVIOUS_APP_ADJ = 7;

    // This is a process holding the home application -- we want to try
    // avoiding killing it, even if it would normally be in the background,
    // because the user interacts with it so much.
	// 主界面进程
    static final int HOME_APP_ADJ = 6;

    // This is a process holding an application service -- killing it will not
    // have much of an impact as far as the user is concerned.
	// 持有应用服务的进程
    static final int SERVICE_ADJ = 5;

    // This is a process with a heavy-weight application.  It is in the
    // background, but we want to try to avoid killing it.  Value set in
    // system/rootdir/init.rc on startup.
	// 重量级应用进程
    static final int HEAVY_WEIGHT_APP_ADJ = 4;

    // This is a process currently hosting a backup operation.  Killing it
    // is not entirely fatal but is generally a bad idea.
	// 执行备份操作的进程
    static final int BACKUP_APP_ADJ = 3;

    // This is a process only hosting components that are perceptible to the
    // user, and we really want to avoid killing them, but they are not
    // immediately visible. An example is background music playback.
	// 拥有用户可感知组件的进程
    static final int PERCEPTIBLE_APP_ADJ = 2;

    // This is a process only hosting acti
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值