OOM killer, oomkiller
In Linux, there is an oom killer mechanism that enables selective kill of some processes when the system memory is exhausted. 1. Why OOM killer?
When we use an application, we need to apply for memory, that is, to perform the malloc operation. If a non-NULL operation is returned, the application has applied for available memory. In fact, there may be bugs in this place. Linux has a memory optimization mechanism, that is, it allows programs to apply for more memory than the available system memory, but Linux does not guarantee that the memory is available immediately, if the memory you applied for is not completely released when you need to use it, OOM killer will be triggered. The kernel code is mm/oom_kill.c. The Calling sequence is as follows:
Malloc-> _ alloc_pages-> out_of_memory ()-> select_bad_process ()-> badness ()
2. How to select the process to kill
Analyze the badness code. The selection process is as follows:
1) Calculate the memory occupied by the process and its subprocesses;
2) Calculate the CPU time and survival time
3) Adjust the weights accordingly.
In summary, the higher the memory usage, the higher the score, the higher the cpu time and survival time, the lower the score; the higher the process priority, the lower the score
Based on the above factors, a point value is obtained, and the highest score is selected and kill.