2.8. Swapping and Out of Memory Tips
Swapping pages out to disk can introduce latency in any environment. To ensure low latency, the best strategy is to have enough memory in your systems so that swapping is not necessary. Always size the physical RAM as appropriate for your application and system. Use
vmstat to monitor memory usage and watch the
si (swap in) and
so (swap out) fields. It is optimal that they remain zero as much as possible.
Procedure 2.8. Out of Memory (OOM)
/proc/sys/vm/panic_on_oom. When set to
1the kernel will panic on OOM. The default setting is
0which instructs the kernel to call a function named
oom_killeron an OOM. Usually,
oom_killercan kill rogue processes and the system will survive.
- The easiest way to change this is to
echothe new value to
cat /proc/sys/vm/panic_on_oom0 ~]#
echo 1 > /proc/sys/vm/panic_on_oom~]#
NoteIt is recommended that you make the $RT; kernel panic on OOM. When the system has encountered an OOM state, it is no longer deterministic.
- It is also possible to prioritize which processes get killed by adjusting the
/proc/PID/there are two files named
oom_score. Valid scores for
oom_adjare in the range -16 to +15. This value is used to calculate the 'badness' of the process using an algorithm that also takes into account how long the process has been running, among other factors. To see the current
oom_killerscore, view the
oom_scorefor the process.
oom_killerwill kill processes with the highest scores first.This example adjusts the
oom_scoreof a process with a PID of 12465 to make it less likely that
oom_killerwill kill it.
cat /proc/12465/oom_score79872 ~]#
echo -5 > /proc/12465/oom_adj~]#
- There is also a special value of -17, which disables
oom_killerfor that process. In the example below,
oom_scorereturns a value of
O, indicating that this process would not be killed.
cat /proc/12465/oom_score78 ~]#
echo -17 > /proc/12465/oom_adj~]#
For more information, or for further reading, the following man pages are related to the information given in this section.