According to https://www.kernel.org/doc/Documentation/sysctl/vm.txt
max_map_count: the maximum number of memory map areas a process may have
The concept of “memory map areas” might be a bit abstract, but it can be made clearer with the help of pmap
. Here is a C++ program, using mmap
to manipulate some
memory maps, then using pmap
to show the effect. Basically, consecutive maps of the same type are automatically merged to be resource efficient.
One can use vm.max_map_count
to bump up the max number of maps a process can construct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include <iostream> #include <sys/mman.h> #include <sys/types.h> #include <unistd.h> using namespace std;static pid_t pid;static void snapshot (int i) { constexpr auto len = 1024 ; char cmd[len]; snprintf (cmd, len, "pmap %d > %d.txt" , pid, i); system (cmd); } int main (void ) { pid = ::getpid (); puts ("clean start" ); snapshot (1 ); puts ("allocating 12k mem" ); constexpr auto size = 12 * 1024 ; auto m = mmap (nullptr , size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0 ); snapshot (2 ); puts ("new map created" ); system ("diff 1.txt 2.txt" ); puts ("releasing the middle 4k mem" ); auto middle = (char *)m + 4 * 1024 ; munmap (middle, 4 * 1024 ); snapshot (3 ); puts ("12k mapping broken into two 4k mapping" ); system ("diff 2.txt 3.txt" ); puts ("allocating the middle 4k mem again" ); mmap (middle, 4 * 1024 , PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0 ); snapshot (4 ); puts ("back to one 12k mapping" ); system ("diff 3.txt 4.txt" ); return 0 ; }
The output of running the above program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 > clang++ test.cc; ./a.out clean start allocating 12k mem new map created 32a33 > 00007f7440430000 12K rw--- [ anon ] 42c43 < total 5568K --- > total 5580K releasing the middle 4k mem 12k mapping broken into two 4k mapping 33c33,34 < 00007f7440430000 12K rw--- [ anon ] --- > 00007f7440430000 4K rw--- [ anon ] > 00007f7440432000 4K rw--- [ anon ] 43c44 < total 5580K --- > total 5576K allocating the middle 4k mem again back to one 12k mapping 33,34c33 < 00007f7440430000 4K rw--- [ anon ] < 00007f7440432000 4K rw--- [ anon ] --- > 00007f7440430000 12K rw--- [ anon ] 44c43 < total 5576K --- > total 5580K