How to prevent a process to be a target of oom-killer?

环境

  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 7

问题

  • Important applications killed by oom-killer when there's memory starvation
  • How could we prevent this process killed by oom-killer

决议

  • This can be achived by using '-17' in oom_adj (RHEL5) or oom_score_adj (RHEL6/7).
  • Here's the way to set it.
  • RHEL5

​Raw​

$ echo '-17' > /proc/<pid>/oom_adj

  • RHEL6/7

​Raw​

$ echo '-1000' > /proc/<pid>/oom_score_adj

  • From /usr/share/doc/kernel-doc-/Documentation/filesystems/proc.txt

​Raw​

For backwards compatibility with previous kernels, /proc/<pid>/oom_adj may also
be used to tune the badness score. Its acceptable values range from -16
(OOM_ADJUST_MIN) to +15 (OOM_ADJUST_MAX) and a special value of -17
(OOM_DISABLE) to disable oom killing entirely for that task. Its value is
scaled linearly with /proc/<pid>/oom_score_adj.

The value of /proc/<pid>/oom_score_adj is added to the badness score before it
is used to determine which task to kill. Acceptable values range from -1000
(OOM_SCORE_ADJ_MIN) to +1000 (OOM_SCORE_ADJ_MAX). This allows userspace to
polarize the preference for oom killing either by always preferring a certain
task or completely disabling it. The lowest possible value, -1000, is
equivalent to disabling oom killing entirely for that task since it will always
report a badness score of 0.