overcommit-accounting.rst 2.6 KB

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