linux 下释放cache、swap

当在Linux下频繁存取文件后,物理内存会很快被用光 ……
出于某种考虑,想手工释放cache、swap,可以手工执行如下操作(尽量少用比较好,呵呵……)


cache释放:
/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


虚拟内存的释放一般都是伴随着关闭应用程式来说的

可以通过命令刷新swap还原到初始状态:
swapoff -a
swapon -a


例如:
[root@wjlcn1026 vm]# free -m
             total       used       free     shared    buffers     cached
Mem:          1744        913        830          0          0        491
-/+ buffers/cache:        422       1321
Swap:         2015       1097        918

[root@wjlcn1026 vm]# swapoff -a
[root@wjlcn1026 vm]# free -m
             total       used       free     shared    buffers     cached
Mem:          1744       1109        634          0          5        694
-/+ buffers/cache:        410       1333
Swap:            0          0          0


[root@wjlcn1026 vm]# swapon -a
[root@wjlcn1026 vm]# free -m
             total       used       free     shared    buffers     cached
Mem:          1744       1116        627          0          5        694
-/+ buffers/cache:        417       1326
Swap:         2015          0       2015

你可能感兴趣的:(linux)