Android的Low Memory Killer和OOM的关系

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/johnWcheung/article/details/87600413

首先,我们要明白OOM是一种异常,LMK是Android系统上的一种防止OOM的内存保护机制。
在这里插入图片描述

关于LowMemoryKiller

Andorid的Low Memory Killer是在标准的linux lernel的OOM基础上修改而来的一种内存管理机制。

使用LowMemoryKiller可以使系统内存较低时,调出进程管理器结束不必要的人进程释放空间。在安卓中,如果等到真正的OOM时,也许进程管理器就已经没法启动了。

Android在系统内存不足时,会触发LMK,根据应用的进程优先级来决定哪个应用先被杀回收,一般规则为:

  • 进程的oom_adj越大,表示此进程优先级越低,越容易被杀回收;越小,表示进程优先级越高,越不容易被杀回收

  • 普通app进程的oom_adj>=0,系统进程的oom_adj才可能<0

关于应用的进程优先级

  1. 查询应用的进程优先级

    ps -A | grep packageName
    cat /proc/pid/oom_adj

  2. 应用进程优先级分类
    在这里插入图片描述

其中每个程序都会有一个oom_adj值,这个值越小,程序越重要,被杀的可能性越低。

除了上述程序重要性分类之外,Android系统还维护着另外一张表,这张表是一个对应关系,以N1为例:
在这里插入图片描述
这个表是定义了一个对应关系,每一个警戒值对应了一个重要性值,当系统的可用内存低于某个警戒值时,就杀掉所有大于该警戒值对应的重要性值的程序。 比如说,当可用内存小于6144 * 4K = 24MB时,开始杀所有的EMPTY_APP,当可用内存小于5632 * 4K = 22MB时,开始杀所有的CONTENT_PROVIDER和EMPTY_APP。

  • UNKNOWN_ADJ = 16
    预留的最低级别,一般对于缓存的进程才有可能设置成这个级别

    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.)

  • CACHED_APP_MAX_ADJ = 15
    缓存进程,空进程,在内存不足的情况下就会优先被kill

    This is a process only hosting activities that are not visible, so it can be killed without any disruption.

  • CACHED_APP_MIN_ADJ = 9
    缓存进程,也就是空进程

  • SERVICE_B_ADJ = 8
    不活跃的进程

    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.

  • PREVIOUS_APP_ADJ = 7
    切换进程

    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.

  • HOME_APP_ADJ = 6
    与Home交互的进程

    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.

  • SERVICE_ADJ = 5
    有Service的进程

    This is a process holding an application service – killing it will not have much of an impact as far as the user is concerned.

  • HEAVY_WEIGHT_APP_ADJ = 4
    高权重进程

    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.

  • BACKUP_APP_ADJ = 3
    正在备份的进程

    This is a process currently hosting a backup operation. Killing it is not entirely fatal but is generally a bad idea.

  • PERCEPTIBLE_APP_ADJ = 2
    可感知的进程,比如那种播放音乐

    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.

  • VISIBLE_APP_ADJ = 1
    可见进程

    This is a process only hosting activities that are visible to the user, so we’d prefer they don’t disappear.

  • FOREGROUND_APP_ADJ = 0
    前台进程

    This is the process running the current foreground app. We’d really rather not kill it!

  • PERSISTENT_SERVICE_ADJ = -11
    重要进程

    This is a process that the system or a persistent process has bound to, and indicated it is important.

  • PERSISTENT_PROC_ADJ = -12
    核心进程

    This is a system persistent process, such as telephony. Definitely don’t want to kill it, but doing so is not completely fatal.

  • SYSTEM_ADJ = -16
    系统进程

    The system process runs at the default adjustment.

  • NATIVE_ADJ = -17
    系统起的Native进程

    Special code for native processes that are not being managed by the system (so don’t have an oom adj assigned by the system).

OOM祸源

  1. 内存泄漏

    长期持有理应释放对象的引用,会导致堆内存不断累积,从而引发OOM异常,Android大部分内存异常主要由于静态持有外部对象的引用,导致外部对象无法及时释放,以及Android组件或集合对象没有被及时关闭。

  2. 大对象的处理的不当

    Android 大的Java对象主要体现在Bitmap对象,因此一定要处理好Bitmap对象,尤其是大量的Bitmap对象的产生。尽可能内存复用、使用缓存。

猜你喜欢

转载自blog.csdn.net/johnWcheung/article/details/87600413