overcommit-accounting 2.5 KB

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