Chinaunix
标题:
内存文件映射的奇怪问题!
[打印本页]
作者:
xzsxiao213
时间:
2008-11-07 13:41
标题:
内存文件映射的奇怪问题!
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#define uchar unsigned char
#define USER_PATH "/home/user"
#define LTH_USER 48
int main()
{
uchar *from, temp;
uchar buf[100];
long i;
int fd;
struct stat statbuf;
fd = open(USER_PATH, O_RDWR);
if (fd)
{
//获取文件大小
if ((fstat(fd, &statbuf)) < 0) //获取失败
{
close (fd);
exit(1);
}
from = (uchar *)mmap(NULL, (size_t)statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (from == MAP_FAILED) //映射失败
{
close(fd);
exit(1);
}
}
else
{
exit(1);
}
printf("====================================\n");
system("free");
for (i = 0; i < 140000; i++)
{
memcpy(buf, from + i*LTH_USER, LTH_USER);
if (buf[47] == 0x01)
continue;
buf[47] = 0x01;
}
printf("====================================\n");
system("free");
munmap(from, (size_t)statbuf.st_size);
printf("====================================\n");
system("free");
close (fd);
printf("====================================\n");
system("free");
}
复制代码
上面代码的运行结果如下:
====================================
total used free shared buffers
Mem: 62676 4908 57768 0 4
Swap: 0 0 0
Total: 62676 4908 57768
====================================
total used free shared buffers
Mem: 62676 11480 51196 0 4
Swap: 0 0 0
Total: 62676 11480 51196
====================================
total used free shared buffers
Mem: 62676 11480 51196 0 4
Swap: 0 0 0
Total: 62676 11480 51196
====================================
total used free shared buffers
Mem: 62676 11480 51196 0 4
Swap: 0 0 0
Total: 62676 11480 51196
我的问题是,为什么内存没有释放掉?(注:被映射的文件比较大6720480 bytes)
内核2.4.18, ARM9开发平台上测试
作者:
timespace
时间:
2008-11-07 22:40
即使嵌入式设备也算个多任务系统吧,看系统物理内存似乎不妥。看进程的物理内存,内存core image和虚拟内存合理些,我用"ps -p pid -o rss,sz,vsize"替换了free,符合预期,结果如下:
====================================
RSS SZ VSZ
324 3372 13488
====================================
RSS SZ VSZ
6888 3372 13488
====================================
RSS SZ VSZ
324 812 3248
====================================
RSS SZ VSZ
324 812 3248
作者:
xzsxiao213
时间:
2008-11-07 23:49
http://linux.chinaunix.net/bbs/thread-1043554-1-1.html
我在另外版块问了这个问题,看来,我用free看到统计的内存是没什么好担心的了,只要程序按正常的操作就可以了!
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2