Search This Blog

Tuesday 4 October 2016

How to clear Linux buffer and cache space

If you ever want to empty it you can use this chain of commands.

[root@test ~]# free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m
total used free shared buffers cached
Mem: 5855 4469 1385 0 21 952
-/+ buffers/cache: 3495 2360
Swap: 4095 50 4045
total used free shared buffers cached
Mem: 5855 4412 1442 0 0 917
-/+ buffers/cache: 3493 2361
Swap: 4095 50 4045

You can signal the Linux Kernel to drop various aspects of cached items by changing the numeric argument to the above command.

  • To free pagecache:

    # echo 1 > /proc/sys/vm/drop_caches  
  • To free dentries and inodes:

    # echo 2 > /proc/sys/vm/drop_caches  
  • To free pagecache, dentries and inodes:

    # echo 3 > /proc/sys/vm/drop_caches  

Non root users run with Sudo:

$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'  $ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'  $ sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'  

NOTE: There's a more esoteric version of the above command if you're into that:

$ echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh  

Why the change in syntax? The /bin/echo program is running as root, because of sudo, but the shell that's redirecting echo's output to the root-only file is still running as you. Your current shell does the redirection before sudo starts.

No comments:

Post a Comment