How does the OOM-Killer select a task to kill?

环境

  • Red Hat Enterprise Linux ( All version)

问题

  • How does the OOM-Killer select a task to kill?

决议

When a system runs out of memory and OOM-killer is needed to set memory free in order to allow new memory allocations, any process in the system's task list is a candidate to be killed. This does not necessarily mean that the process which has the biggest virtual memory area will be a direct first hit, however it has its chances of being killed as well.

When the routine OOM-killer is called, it iterates over the task list calculating a score, called 'badness', for each process enlisted there. In its context the badness calculator utilizes a fairly simple formula that tries to select the better task to kill in a system following this logic:

  1. try to lose the minimum amount of work done;
  2. permit to recover a large amount of memory;
  3. try to not kill anything innocent of allocating large amount of memory;
  4. try to kill the minimum amount of tasks at a time (one process is always the goal);
  5. try to kill what end-users expect the kernel should kill;

When OOM-killer is calculating a given process badness, the first thing taken into account is the process total virtual memory size (vm), in pages, and this number will serve as the base for all further OOM-killer 'badness' score calculations. While one might think that processes which have big "vms" are prone to be first selected by OOM-killer, the calculations also take into consideration other data to adjust a process score, thus giving to it a chance of not being killed even if it is the biggest memory allocator in the box.

Below, these are other things that will adjust a process 'badness' score:

  • Forked childs: for each child which has its own 'vm', a half of that vm is accounted to a task points score;
  • CPU time: a task gets its score reduced by a factor rendered from the integer part of the square root of its CPU running time. So last longing tasks or tasks runing often are adjusted to score less, even if they are allocating big chunks of memory;
  • Niced processes: a niced process is considered less important, so they get their score doubled;
  • Superuser processes: As they are usually considered more important, they get their score reduced by a factor of 4;
  • External adjustment: System administrator can do external adjustments to this calculations, in order to prevent oom-killer to kill an undesirable process which otherwise would be killed in a memory shortage.

Further information on OOM-Killer: