在Linux下,内存不会自动释放。当频繁读取文件过多之后就会出现free内存很小的表象,效果如下:
请注意,上图显示已使用内存:7364432,是包含后面buffers和cached的。并不代表实际物理内存使用到了7.3G,实际使用的物理内存是下面显示去掉buffers和cache后的:828144。我们看到cached居然使用了6G之多,其实就是上面说的内存没有自动释放而已。。如果想手动释放内存,请执行下面命令即可。
释放内存:
/bin/sync
/bin/echo "1">/proc/sys/vm/drop_caches
操作之后再来看看内存使用情况:
相关引用如下:
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.