Android进程优先级oom_adj

本文详细介绍了Android系统如何通过oom_adj数值来判断进程的重要程度,并决定是否结束进程以释放内存。此外,还提供了设置服务为前台服务的方法,以提高其在系统内存紧张时的存活概率。
当Android运行了很多进程后,由于内存不足,系统会杀掉一下不重要的进程,提供内存给更重要的进程使用。那么系统是如何对进程的“重要性“进行判定的呢?
命令行中输入cat /proc/process_id/oom_adj 可以查看到某一进程的adj数值,系统就是根据这一数值来判定进程是否被杀或者保留。

ADJ等级定义在frameworks/base/services/core/java/com/android/server/am/ProcessList.java

// 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.
    // B 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 activities that are visible to the
    // user, so we'd prefer they don't disappear.
    // 用户可见的activity进程
    static final int VISIBLE_APP_ADJ = 1;

    // This is the process running the current foreground app.  We'd really
    // rather not kill it!
    // 前台应用进程,永远都会避免杀掉他
    static final int FOREGROUND_APP_ADJ = 0;

    // This is a process that the system or a persistent process has bound to,
    // and indicated it is important.
    // 由系统或持久性进程所绑定的进程
    static final int PERSISTENT_SERVICE_ADJ = -11;

    // This is a system persistent process, such as telephony.  Definitely
    // don't want to kill it, but doing so is not completely fatal.
    // 持久性进程,例如电话
    static final int PERSISTENT_PROC_ADJ = -12;

    // The system process runs at the default adjustment.
    // 系统进程
    static final int SYSTEM_ADJ = -16;

    // Special code for native processes that are not being managed by the system (so
    // don't have an oom adj assigned by the system).
    // native本地进程,不由系统管理,所以不会分配oom_adj
    static final int NATIVE_ADJ = -17;

来看一个实例:
我们经常会有这样的需求:希望我们程序的某一个服务尽可能的不被系统杀掉。我们常用的服务有如下几种,可以通过查看oom_adj来查看他们的优先级。

1.同一进程中的后台服务
这里写图片描述
2.同一进程中的前台服务
这里写图片描述
3.新的进程中的后台进程
这里写图片描述
4.新的进程中的前台进程
这里写图片描述

对比上面四种情况,2和4是比较稳定的,但是使用新的进程启动服务,可以在不需要主进程的时候,手动杀掉主进程而不会影响到服务进程,是应用内存使用进一步降低。因此,4应该是最好的方法。

前台进程:
一定要有一个notification与之相绑定。

    // 设置为前台服务
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("tag", "MusicService onStartCommand");
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
        builder.setSmallIcon(R.drawable.ic_music_note_white_18dp);
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_music_note_white_36dp));
        builder.setContentTitle("下拉通知中的标题");
        builder.setContentText("下拉通知中要显示的内容");
        builder.setWhen(System.currentTimeMillis());
        Intent clickIntent = new Intent(this, MainActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, clickIntent, 0));
        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND;

        startForeground(100, notification);
        return super.onStartCommand(intent, flags, startId);
    }
   // 结束前台服务
   @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true); //参数表示是否移除通知
    }
在 Linux 系统中,`oom_adj`、`oom_score` 和 `oom_score_adj` 是与 **OOM Killer(Out-of-Memory Killer)** 相关的进程级参数,用于控制进程在内存不足时被内核终止的优先级。以下是它们的详细解释和关系: --- ### 1. **参数定义与作用** #### (1)`oom_score_adj`(推荐使用) - **路径**:`/proc/[pid]/oom_score_adj` - **范围**:`-1000` 到 `1000` - **作用**: - 调整进程OOM 优先级,**值越低**(如 `-1000`)越**不可能**被杀死,**值越高**(如 `1000`)越**容易被杀死**。 - 替代旧版的 `oom_adj`,提供更精细的控制(支持负值)。 - **示例**: - 设为 `-1000`:进程几乎不会被 OOM Killer 终止(如关键系统进程)。 - 设为 `1000`:进程会优先被杀死(如内存密集型后台任务)。 #### (2)`oom_adj`(旧版,已废弃) - **路径**:`/proc/[pid]/oom_adj` - **范围**:`-16` 到 `15` - **作用**: - 旧版 Linux 用于调整 OOM 优先级,但范围有限且不支持负值(`-17` 表示禁止 OOM Killer,但实际无效)。 - **已废弃**,建议使用 `oom_score_adj`。 - **转换关系**: 内核内部会将 `oom_adj` 转换为 `oom_score_adj`,公式为: ```math oom\_score\_adj = oom\_adj \times \frac{17}{1000} ``` 但直接写入 `oom_adj` 时,内核会按比例映射到 `oom_score_adj` 的范围(如 `-16` → `-1000`,`15` → `1000`)。 #### (3)`oom_score`(只读) - **路径**:`/proc/[pid]/oom_score` - **范围**:`0` 到 `∞` - **作用**: - 表示进程当前的 **OOM 杀伤分数**,由内核根据以下因素计算: - 进程的 `oom_score_adj` 值。 - 进程占用的内存大小(RSS,实际物理内存)。 - 其他内核内部规则(如进程的 UID、是否为内核线程等)。 - **值越高**,进程越容易被杀死。 --- ### 2. **参数关系与计算** #### (1)`oom_adj` 与 `oom_score_adj` 的转换 用户提供的 `oom_adj` 值会通过以下公式转换为 `oom_score_adj`: ```math oom\_score\_adj = oom\_adj \times \frac{17}{1000} ``` 但实际内核处理时,会按比例缩放到 `oom_score_adj` 的完整范围(`-1000` 到 `1000`)。例如: - `oom_adj = -16` → `oom_score_adj = -1000`(禁止杀死) - `oom_adj = 15` → `oom_score_adj = 1000`(最高优先级杀死) #### (2)`oom_score` 的计算 内核根据以下公式计算 `oom_score`: ```math oom\_score = \text{进程内存占用} \times \text{调整因子} + \text{其他修正项} ``` 其中 **调整因子** 由 `oom_score_adj` 决定: - 若 `oom_score_adj > 0`:`oom_score` 会显著增加(容易被杀)。 - 若 `oom_score_adj < 0`:`oom_score` 会减小(受保护)。 --- ### 3. **用户示例分析** 假设某进程的参数如下: ```bash oom_adj: -13 oom_score: 0 oom_score_adj: -800 ``` #### (1)验证 `oom_adj` 与 `oom_score_adj` 的关系 根据公式: ```math oom\_score\_adj = oom\_adj \times \frac{17}{1000} = -13 \times \frac{17}{1000} = -0.221 ``` 但实际 `oom_score_adj` 显示为 `-800`,说明: - 用户直接写入了 `oom_score_adj`(而非通过 `oom_adj` 间接设置)。 - 此时 `oom_adj` 的值是内核根据 `oom_score_adj` 反向计算得出的(不精确,仅用于兼容旧接口)。 #### (2)`oom_score` 为 0 的原因 - 进程可能占用内存极少,或 `oom_score_adj` 的负值足够大,使得最终分数被内核修正为 0(表示受强保护)。 - 内核可能对关键进程(如 UID=0 的进程)有额外保护逻辑。 --- ### 4. **关键注意事项** 1. **优先使用 `oom_score_adj`**: - `oom_adj` 已废弃,且范围有限(`-16` 到 `15`),无法提供精细控制(如 `-800`)。 2. **`oom_score` 是结果,非配置项**: - 用户只能通过调整 `oom_score_adj` 间接影响 `oom_score`。 3. **内核保护机制**: - 即使 `oom_score_adj` 设为 `-1000`,某些进程(如内核线程)仍可能被杀死。 4. **Android 的特殊处理**: - Android 的 `lmkd` 会动态调整 `oom_score_adj`,优先级可能与普通 Linux 不同(如更激进的回收策略)。 --- ### 5. **实际应用场景** #### (1)保护关键进程 ```bash echo -1000 > /proc/[pid]/oom_score_adj # 禁止被 OOM Killer 杀死 ``` #### (2)强制杀死高内存占用进程 ```bash echo 1000 > /proc/[pid]/oom_score_adj # 标记为最高优先级杀死 ``` #### (3)查看当前 OOM 优先级 ```bash cat /proc/[pid]/oom_score # 查看实际分数 cat /proc/[pid]/oom_score_adj # 查看调整值 ``` --- ### 6. **与 `lowmemorykiller` 的关系** - Android 的 `lowmemorykiller` 直接读取 `oom_score_adj`,而非依赖 `oom_score`。 - 当系统内存不足时,`lowmemorykiller` 会按 `oom_score_adj` 的阈值(如 `adj` 参数)杀死进程,而非依赖内核的 `oom_killer`。 --- ### 示例命令总结 | 操作 | 命令 | |------|------| | 禁止进程被杀死 | `echo -1000 > /proc/[pid]/oom_score_adj` | | 标记为高优先级杀死 | `echo 1000 > /proc/[pid]/oom_score_adj` | | 查看当前分数 | `cat /proc/[pid]/oom_score` | | 查看调整值 | `cat /proc/[pid]/oom_score_adj` | --- ### 关键问题与注意事项 1. **为什么 `oom_score` 可能为 0?** - 进程内存占用极低,或 `oom_score_adj` 的负值完全抵消了内存权重。 2. **`oom_adj` 和 `oom_score_adj` 的优先级?** - 直接写入 `oom_score_adj` 会覆盖 `oom_adj` 的设置,反之则不成立(因 `oom_adj` 已废弃)。 3. **内核如何处理 `oom_score_adj = -1000` 的进程?** - 通常跳过 OOM Killer,但极端情况下(如系统完全卡死)仍可能被杀死。 4. **Android 与普通 Linux 的 OOM 行为差异?** - Android 的 `lowmemorykiller` 主动管理进程,而非完全依赖内核的 `oom_killer`。 5. **`oom_score` 的计算公式是否公开?** - 内核源码中定义(如 `mm/oom_kill.c`),但具体实现可能因版本而异。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值