overcommit-accounting.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. =====================
  2. Overcommit Accounting
  3. =====================
  4. The Linux kernel supports the following overcommit handling modes
  5. 0
  6. Heuristic overcommit handling. Obvious overcommits of address
  7. space are refused. Used for a typical system. It ensures a
  8. seriously wild allocation fails while allowing overcommit to
  9. reduce swap usage. root is allowed to allocate slightly more
  10. memory in this mode. This is the default.
  11. 1
  12. Always overcommit. Appropriate for some scientific
  13. applications. Classic example is code using sparse arrays and
  14. just relying on the virtual memory consisting almost entirely
  15. of zero pages.
  16. 2
  17. Don't overcommit. The total address space commit for the
  18. system is not permitted to exceed swap + a configurable amount
  19. (default is 50%) of physical RAM. Depending on the amount you
  20. use, in most situations this means a process will not be
  21. killed while accessing pages but will receive errors on memory
  22. allocation as appropriate.
  23. Useful for applications that want to guarantee their memory
  24. allocations will be available in the future without having to
  25. initialize every page.
  26. The overcommit policy is set via the sysctl ``vm.overcommit_memory``.
  27. The overcommit amount can be set via ``vm.overcommit_ratio`` (percentage)
  28. or ``vm.overcommit_kbytes`` (absolute value). These only have an effect
  29. when ``vm.overcommit_memory`` is set to 2.
  30. The current overcommit limit and amount committed are viewable in
  31. ``/proc/meminfo`` as CommitLimit and Committed_AS respectively.
  32. Gotchas
  33. =======
  34. The C language stack growth does an implicit mremap. If you want absolute
  35. guarantees and run close to the edge you MUST mmap your stack for the
  36. largest size you think you will need. For typical stack usage this does
  37. not matter much but it's a corner case if you really really care
  38. In mode 2 the MAP_NORESERVE flag is ignored.
  39. How It Works
  40. ============
  41. The overcommit is based on the following rules
  42. For a file backed map
  43. | SHARED or READ-only - 0 cost (the file is the map not swap)
  44. | PRIVATE WRITABLE - size of mapping per instance
  45. For an anonymous or ``/dev/zero`` map
  46. | SHARED - size of mapping
  47. | PRIVATE READ-only - 0 cost (but of little use)
  48. | PRIVATE WRITABLE - size of mapping per instance
  49. Additional accounting
  50. | Pages made writable copies by mmap
  51. | shmfs memory drawn from the same pool
  52. Status
  53. ======
  54. * We account mmap memory mappings
  55. * We account mprotect changes in commit
  56. * We account mremap changes in size
  57. * We account brk
  58. * We account munmap
  59. * We report the commit status in /proc
  60. * Account and check on fork
  61. * Review stack handling/building on exec
  62. * SHMfs accounting
  63. * Implement actual limit enforcement
  64. To Do
  65. =====
  66. * Account ptrace pages (this is hard)