Cannot allocate memory when overcommit_memory=1

324 Views Asked by At

I have /proc/sys/vm/overcommit_memory set to 1, which I thought would allow any amount of memory overcommit, up to the range allowed by a 64bit pointer. However, when my memory request becomes large enough, around 86Tb, the mmap call begins to fail. Is this an actual upper limit I'm hitting and if so what is it? Or am I making some mistake in allocating the memory?

The code below reproduces the issue on my laptop, the memory threshold may need to be tweaked on other computers.

#include<iostream>

#include <sys/mman.h>

static constexpr const size_t span = 86ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;

int main() {
    fprintf(stderr, "ALLOCATING %ld bytes\n", span);
    if (mmap(0, span, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0) == MAP_FAILED) {
        perror("...");
        exit(-4);
    }
}

Console output

[user@hp]$
[user@hp]$ cat /proc/sys/vm/overcommit_memory
1
[user@hp]$ g++ -std=c++2a -o test test.cpp
[user@hp]$ ./test
ALLOCATING 94557999988736 bytes
...: Cannot allocate memory

This is the output of prlimit:

[user@hp]$ prlimit
RESOURCE   DESCRIPTION                             SOFT      HARD UNITS
AS         address space limit                unlimited unlimited bytes
CORE       max core file size                 unlimited unlimited bytes
CPU        CPU time                           unlimited unlimited seconds
...
0

There are 0 best solutions below