[英]How can mmap allocate more than 20Gb?
I try using mmap function to allocate a large amount of memory in virtual space. 我尝试使用mmap函数在虚拟空间中分配大量内存。 My need is about of 30Gb, but it can't.
我需要大约30Gb,但它不能。 I tried with 20Gb with the same result.
我用20Gb尝试了相同的结果。 I executed my test on OVH 64 bits machine server with 60Gb RAM.
我在具有60Gb RAM的OVH 64位机器服务器上执行了测试。
My test code: 我的测试代码:
#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;
}
The result of execution: 执行结果:
8
1 Cannot allocate memory
Someone can tell me if it's possible or not to allocate 30Gb of virtual memory with mmap, and why ? 有人可以告诉我是否可以使用mmap分配30Gb的虚拟内存,为什么? Elsewhat how to allocate 30Gb with another way ?
那么如何用另一种方式分配30Gb呢?
Please, don't ask why i want to do that or it's stupid or another philosophic thoughts. 请不要问我为什么要这样做,或者是愚蠢的或其他哲学思想。 It's my need and i just want to find a way to do it if it's possible.
这是我的需要,我只想找到一种方法,如果可能的话。
I ran your program on Linux Mint 17 (roughly Ubuntu 14.04) x86_64 without error. 我在Linux Mint 17(大致是Ubuntu 14.04)x86_64上运行你的程序没有错误。 My system has 8G of RAM, 16G of swap, most of the 8G currently used by page cache, and 1-2GB currently used by processes.
我的系统有8G的RAM,16G的交换,当前由页面缓存使用的大部分8G,以及当前由进程使用的1-2GB。
(Incidentally, for questions like this, you really need to specify your platform in detail.) (顺便说一句,对于这样的问题,您确实需要详细指定您的平台。)
I was able to reproduce your error message in two different ways. 我能够以两种不同的方式重现您的错误消息。
First way: 第一种方式:
$ ./mmap
8
0 Success
$ ulimit -v 65536
$ ./mmap
8
1 Cannot allocate memory
Second way (from a new shell): 第二种方式(来自新的shell):
$ ./mmap
8
0 Success
$ sysctl vm.overcommit_memory
vm.overcommit_memory = 0
$ sudo sysctl vm.overcommit_memory=2
vm.overcommit_memory = 2
$ ./mmap
8
1 Cannot allocate memory
So, as @vz0 asked, what does ulimit -a
say? 所以,正如@ vz0所问,
ulimit -a
说什么?
How much swap do you have, how much memory is in use, and how does your system have vm.overcommit_memory
set? 你有多少交换,正在使用多少内存,以及你的系统如何
vm.overcommit_memory
?
I am going to go out on a limb a bit and assert that one of these is your culprit. 我会稍微走出去,断言其中一个是你的罪魁祸首。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.