86 lines · plain
1=====================2Overcommit Accounting3=====================4 5The Linux kernel supports the following overcommit handling modes6 708 Heuristic overcommit handling. Obvious overcommits of address9 space are refused. Used for a typical system. It ensures a10 seriously wild allocation fails while allowing overcommit to11 reduce swap usage. This is the default.12 13114 Always overcommit. Appropriate for some scientific15 applications. Classic example is code using sparse arrays and16 just relying on the virtual memory consisting almost entirely17 of zero pages.18 19220 Don't overcommit. The total address space commit for the21 system is not permitted to exceed swap + a configurable amount22 (default is 50%) of physical RAM. Depending on the amount you23 use, in most situations this means a process will not be24 killed while accessing pages but will receive errors on memory25 allocation as appropriate.26 27 Useful for applications that want to guarantee their memory28 allocations will be available in the future without having to29 initialize every page.30 31The overcommit policy is set via the sysctl ``vm.overcommit_memory``.32 33The overcommit amount can be set via ``vm.overcommit_ratio`` (percentage)34or ``vm.overcommit_kbytes`` (absolute value). These only have an effect35when ``vm.overcommit_memory`` is set to 2.36 37The current overcommit limit and amount committed are viewable in38``/proc/meminfo`` as CommitLimit and Committed_AS respectively.39 40Gotchas41=======42 43The C language stack growth does an implicit mremap. If you want absolute44guarantees and run close to the edge you MUST mmap your stack for the45largest size you think you will need. For typical stack usage this does46not matter much but it's a corner case if you really really care47 48In mode 2 the MAP_NORESERVE flag is ignored.49 50 51How It Works52============53 54The overcommit is based on the following rules55 56For a file backed map57 | SHARED or READ-only - 0 cost (the file is the map not swap)58 | PRIVATE WRITABLE - size of mapping per instance59 60For an anonymous or ``/dev/zero`` map61 | SHARED - size of mapping62 | PRIVATE READ-only - 0 cost (but of little use)63 | PRIVATE WRITABLE - size of mapping per instance64 65Additional accounting66 | Pages made writable copies by mmap67 | shmfs memory drawn from the same pool68 69Status70======71 72* We account mmap memory mappings73* We account mprotect changes in commit74* We account mremap changes in size75* We account brk76* We account munmap77* We report the commit status in /proc78* Account and check on fork79* Review stack handling/building on exec80* SHMfs accounting81* Implement actual limit enforcement82 83To Do84=====85* Account ptrace pages (this is hard)86