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