overcommit-accounting 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 percentage (default is 50) of physical RAM.
  15. Depending on the percentage 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 percentage is set via `vm.overcommit_ratio'.
  24. The current overcommit limit and amount committed are viewable in
  25. /proc/meminfo as CommitLimit and Committed_AS respectively.
  26. Gotchas
  27. -------
  28. The C language stack growth does an implicit mremap. If you want absolute
  29. guarantees and run close to the edge you MUST mmap your stack for the
  30. largest size you think you will need. For typical stack usage this does
  31. not matter much but it's a corner case if you really really care
  32. In mode 2 the MAP_NORESERVE flag is ignored.
  33. How It Works
  34. ------------
  35. The overcommit is based on the following rules
  36. For a file backed map
  37. SHARED or READ-only - 0 cost (the file is the map not swap)
  38. PRIVATE WRITABLE - size of mapping per instance
  39. For an anonymous or /dev/zero map
  40. SHARED - size of mapping
  41. PRIVATE READ-only - 0 cost (but of little use)
  42. PRIVATE WRITABLE - size of mapping per instance
  43. Additional accounting
  44. Pages made writable copies by mmap
  45. shmfs memory drawn from the same pool
  46. Status
  47. ------
  48. o We account mmap memory mappings
  49. o We account mprotect changes in commit
  50. o We account mremap changes in size
  51. o We account brk
  52. o We account munmap
  53. o We report the commit status in /proc
  54. o Account and check on fork
  55. o Review stack handling/building on exec
  56. o SHMfs accounting
  57. o Implement actual limit enforcement
  58. To Do
  59. -----
  60. o Account ptrace pages (this is hard)