在Linux系统下,通过命令可以查看内存状态,比如
# more /proc/meminfo
MemTotal: 16423848 kB
MemFree: 3008348 kB
Buffers: 272916 kB
Cached: 12539184 kB
SwapCached: 0 kB
Active: 3473960 kB
Inactive: 9512632 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 16423848 kB
LowFree: 3008348 kB
SwapTotal: 16386292 kB
SwapFree: 16386292 kB
Dirty: 176 kB
Writeback: 0 kB
AnonPages: 174440 kB
Mapped: 52192 kB
Slab: 361836 kB
PageTables: 19220 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 24598216 kB
Committed_AS: 883256 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 274632 kB
VmallocChunk: 34359463459 kB
HugePages_Total: 0
各项的具体含义,这里不具体解释,其中有一项是cached,指的是目前被缓存的内容,可能被下次访问用到,也可能被替换出去,一般都与Linux系统自动管理,不过在做某些测试时,不希望cache干扰,可以通过下面的方法来进行清理,即执行两条命令.
#sync
#echo 3 > /proc/sys/vm/drop_caches
执行完后,再查看内存状态,如下:
# more /proc/meminfo
MemTotal: 16423848 kB
MemFree: 16099556 kB
Buffers: 360 kB
Cached: 52720 kB
...
除了这个方法之外,还在网上找到这么一段程序,据说可以清空某个指定文件的缓存,不过发现对于NFS不起作用,放在这里朋友一块交流交流.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
main(int argc,char **argv)
{
int fd,result;
if (argc < 2){
printf("Usage: %s filename\n", argv[0]);
_exit(-1);
}
printf("Opening: %s\n",argv[1]);
fd = open(argv[1], O_RDWR);
//printf("FD: %d\n",fd);
if (fd < 0){
printf("open file failed\n");
_exit(-1);
}
//result = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
result = posix_fadvise(fd, 0, 0, 4);
printf("Result: %d\n",result);
close(fd);
_exit(0);
}
本文出自 “玩linux…做运维…集群..” 博客,请务必保留此出处http://scottlinn.blog.51cto.com/829656/736894
|