這里就涉及到一個問題,到底Kill掉誰呢?一般稍微了解一些Linux內(nèi)核的同學第一反應是誰用的最多,就Kill掉誰。這當然是Linux內(nèi)核首先考慮的一種重要因素,但是也不完全是這樣的,我們查一些Linux的內(nèi)核方面的資料,可以知道其實Kill誰是由/proc/<pid>/oom_score來決定的,這個值每個進程一個,是由Linux內(nèi)核的oom_badness()函數(shù)負責計算的。那下面我們來仔細讀一讀badness()函數(shù)。
在badness()函數(shù)的注釋部分,寫明了badness()函數(shù)的處理思路:
1) we lose the minimum amount of work done
2) we recover a large amount of memory
3) we don't kill anything innocent of eating tons of memory
4) we want to kill the minimum amount of processes (one)
5) we try to kill the process the user expects us to kill, this algorithm has been meticulously tuned to meet the principle of least surprise ... (be careful when you change it)
總的來說就是Kill掉最小數(shù)量的進程來獲取最大數(shù)量的內(nèi)存,這與我們Kill掉占用內(nèi)存最大的進程是吻合的。
/*
* The memory size of the process is the basis for the badness.
*/
points = p->mm->total_vm;
分數(shù)的起始是進程實際使用的RAM內(nèi)存,注意這里不包括SWAP,即OOM Killer只會與進程實際的物理內(nèi)存有關(guān),與Swap是沒有關(guān)系的,并且我們可以看到,進程實際使用的物理內(nèi)存越多,分數(shù)就越高,分數(shù)越高就越容易被犧牲掉。
/*
* Processes which fork a lot of child processes are likely
* a good choice. We add the vmsize of the childs if they
* have an own mm. This prevents forking servers to flood the
* machine with an endless amount of childs
*/
...
if (chld->mm != p->mm && chld->mm)
points += chld->mm->total_vm;
這段表示子進程占用的內(nèi)存都會計算到父進程上。
s = int_sqrt(cpu_time);
新聞熱點
疑難解答
圖片精選