内存管理中的overcommit
1. 什么是overcommit
Linux calls this overcommit, which refers to the fact that thekernel allows a process to allocate more memory than is currently available.The kernel is effectively speculating that the memory will be available whenneeded.
Linux对大部分申请内存的请求都回复"yes",以便能跑更多更大的程序。因为申请内存后,并不会马上使用内存。这种技术叫做Overcommit。当linux发现内存不足时,会发生OOM killer(OOM=out-of-memory)。它会选择杀死一些进程(用户态进程,不是内核线程),以便释放内存。
当oom-killer发生时,linux会选择杀死哪些进程?选择进程的函数是oom_badness函数(在mm/oom_kill.c中),该函数会计算每个进程的点数(0~1000)。点数越高,这个进程越有可能被杀死。每个进程的点数跟oom_score_adj有关,而且oom_score_adj可以被设置(-1000最低,1000最高)。
2. Linux configure the overcommit
The Linux kernel supports the following overcommit handling modes
0 - Heuristic overcommithandling. Obvious overcommits of address space are refused. Used for a typicalsystem. It ensures a seriously wild allocation fails while allowing overcommitto reduce swap usage. root is allowed toallocate slighly more memory in this mode. This is the default. (表示检查是否有足够的内存可用,如果是,允许分配;如果内存不够,拒绝该请求,并返回一个错误给应用程序。)
1 - Alwaysovercommit. Appropriate for some scientific applications. (表示根据vm.overcommit_ratio定义的值,允许分配超出一定内存(物理内存加上交换内存)的请求。vm.overcommit_ratio参数是一个百分比,加上内存量决定内存可以超量分配多少内存。例如,vm.overcommit_ratio值为50,而内存有1GB,那么这意味着在内存分配请求失败前,加上交换内存,内存将允许高达1.5GB的内存分配请求。)
2 - Don'tovercommit. The total address space commit for the system is not permitted toexceed swap + a configurable percentage (default is 50) of physical RAM. Dependingon the percentage you use, in most situations this means a process wi