Dropping cached memory in Linux
This is kind of a followup to the previous post about Linux seemingly being out of memory.
There is kernel parameter that can be altered on the run to free up memory used up by pagecache, dentries and inodes. In the following outputs we are interested in buffers/cache values. Note that sync command should be run first to flush out all cached objects.
Following is output of free command before anything is done:
root@ultra:~# sync;free
total used free shared buffers cached
Mem: 16446564 9924728 6521836 0 337568 8135860
-/+ buffers/cache: 1451300 14995264
Swap: 1998840 0 1998840
Now by echoing 1 into /proc/sys/vm/drop_caches memory used by pagecache is freed:
root@ultra:~# echo 1 > /proc/sys/vm/drop_caches
root@ultra:~# free
total used free shared buffers cached
Mem: 16446564 1557852 14888712 0 504 150524
-/+ buffers/cache: 1406824 15039740
Swap: 1998840 0 1998840
By echoing 2 into the same file you can free up memory used by dentries and inodes:
root@ultra:~# echo 2 > /proc/sys/vm/drop_caches
root@ultra:~# free
total used free shared buffers cached
Mem: 16446564 1427692 15018872 0 1404 151332
-/+ buffers/cache: 1274956 15171608
Swap: 1998840 0 1998840
The above two steps can be combined into one:
root@ultra:~# echo 3 > /proc/sys/vm/drop_caches
Alternatively sysctl -w vm.drop_caches=3 will achieve the same. Most of the information is described here, but I just do not feel like looking for it every time I need it.