I am trying to use the mmap function to allocate a large amount of memory in virtual space. My need is about 30 GB, but this is not possible. I tried with 20 GB with the same result. I ran my test on a 64-bit OVH server with 60 GB of RAM.
My test code is:
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
int main()
{
void *r = NULL;
printf("%lu\n", sizeof(size_t));
r = mmap(NULL, ((size_t)20)*1024*1024*1024, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
printf("%d %s\n", r == MAP_FAILED, strerror(errno));
return 0;
}
Execution Result:
8
1 Cannot allocate memory
Can someone tell me if it is possible or not to allocate 30 GB of virtual memory using mmap and why? Also how to distribute 30Gb in another way?
Please do not ask why I want to do this or is it stupid or other philosophical thoughts. This is my need, and I just want to find a way to do this, if possible.