Out Of Memory Killer或OOM Killer是内核中一种用于在系统内存紧张的情况下,防止内存耗尽并自动查杀进程的机制。
系统内存紧张往往是由于内核中内存分配过度导致的。当内核检测到系统内存不足的情况下,Linux内核会调用OOM Killer去检查所有正在运行的进程,并从中选择一个或多个进程(bad process)来杀掉以释放一部分系统内存,缓解系统内存紧张的情况,最终确保系统稳定运行。
OOM Killer仅会在系统内存极度紧张的情况下才会被调用。
一、OOM 触发机制当OOM机制被触发时,内核会调用out_of_memory()方法,函数调用链路如下:
1.1 触发OOMlinux_mainline-5.17.0/mm/page_alloc.c
__alloc_pages() --> __alloc_pages_slowpath() --> __alloc_pages_may_oom() --> out_of_memory()
linux_mainline-5.17.0/mm/oom_kill.c 1043 /** 1044 * out_of_memory - kill the "best" process when we run out of memory 1045 * @oc: pointer to struct oom_control 1046 * 1047 * If we run out of memory, we have the choice between either 1048 * killing a random task (bad), letting the system crash (worse) 1049 * OR try to be smart about whi



