1

The following code will generate errno 12 cannot allocate memory

#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>
int main()
{
    char* p;
    for (size_t i = 0; i < 0x10000; i++)
    {
        char* addr = (char*)0xAAA00000000uL - i * 0x2000;
        p = mmap(addr, 0x1000,
            PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p != addr) {
            printf("%lu %d\n", i, errno);
            getchar();
            return 1;
        }
        memset(p, 'A' + (i % 26), 0x1000);
    }
    return 0;
}

The output is 65510 12 on my machine.

However, if we change size of each page from 0x1000 to 0x2000, the allocation will be successful, even if it is using more memory. The only difference I think is the number of continuous pages, is there a limitation on this? If so, how to set it to unlimited?

2019
  • 21
  • 3
  • 1
    There aren't really contiguous _pages_ (page slots) in virtual memory per se. See my answer: https://stackoverflow.com/a/37173063/5382650 then the answer linked in the answer and the two links in the comments below the answer – Craig Estey May 15 '22 at 00:12

1 Answers1

1

It seems that setting /proc/sys/vm/max_map_count to a larger number solves the problem. Reference: How much memory could vm use

2019
  • 21
  • 3