- 论坛徽章:
- 0
|
今天写了一个测试程序,malloc 900多M , mmap 1G 内存,发现mmap总是失败,报错信息显示 “不能分配内存”, free的内存有2G多应该可以分配的,不知道是哪里有限制导致系统无法申请大内存。请大家指点。谢谢。
系统资源如下:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
143G 33G 103G 24% /
/dev/sda1 99M 16M 78M 17% /boot
tmpfs 1.8G 8.0K 1.8G 1% /dev/shm
[root@localhost ~]# free -m
total used free shared buffers cached
Mem: 3541 1397 2144 0 16 1320
-/+ buffers/cache: 59 3481
Swap: 1983 0 1983
[root@localhost ~]# cat /proc/meminfo
MemTotal: 3626092 kB
MemFree: 1377096 kB
Buffers: 18484 kB
Cached: 2152352 kB
SwapCached: 0 kB
Active: 999752 kB
Inactive: 1205352 kB
HighTotal: 2742808 kB
HighFree: 550396 kB
LowTotal: 883284 kB
LowFree: 826700 kB
SwapTotal: 2031608 kB
SwapFree: 2031608 kB
Dirty: 37680 kB
Writeback: 48584 kB
AnonPages: 34260 kB
Mapped: 10104 kB
Slab: 33476 kB
SReclaimable: 19396 kB
SUnreclaim: 14080 kB
PageTables: 1608 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 3844652 kB
Committed_AS: 69200 kB
VmallocTotal: 116728 kB
VmallocUsed: 5212 kB
VmallocChunk: 111060 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
Hugepagesize: 2048 kB
[root@localhost ~]#
代码如下:
[root@localhost ~]# cat t.c
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *p;
int fd;
char *header;
p = malloc(948135260);
if(p==NULL)
return -1;
memset(p, 0, 948135260);
fd = shm_open("ttt", O_RDWR | O_CREAT, 0644);
if (fd == -1)
perror("shm_open");
ftruncate(fd, 1062369960);
header = mmap(NULL, 1062369960, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
if(header == (void*)-1){
perror("mmap");
}else{
memset(header, 0, 1062369960);
}
}
编译:
[root@localhost ~]# gcc t.c -lrt
执行:
[root@localhost ~]# ./a.out
mmap: Cannot allocate memory
[root@localhost ~]#
|
|