Prior to doing some benchmarking work how would one free up the memory (RAM) that the Linux Kernel is consuming for its buffers and cache?
Note that this is mostly useful for benchmarking. Emptying the buffers and cache reduces performance! If you're here because you thought that freeing buffers and cache was a positive thing, go and read Linux ate my RAM!. The short story: free memory is unused memory is wasted memory.
Best Answer
Emptying the buffers cache
If you ever want to empty it you can use this chain of commands.
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:
To free dentries and inodes:
To free pagecache, dentries and inodes:
The above are meant to be run as root. If you're trying to do them using
sudo
then you'll need to change the syntax slightly to something like these:NOTE: There's a more esoteric version of the above command if you're into that:
Why the change in syntax? The
/bin/echo
program is running as root, because ofsudo
, 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 beforesudo
starts.Seeing what's in the buffers and cache
Take a look at
linux-ftools
if you'd like to analyze the contents of the buffers & cache. Specifically if you'd like to see what files are currently being cached.fincore
With this tool you can see what files are being cached within a give directory.
For example,
/var/lib/mysql/blogindex
:With the above output you can see that there are several *.MYD, *.MYI, and *.frm files that are currently being cached.
Swap
If you want to clear out your swap you can use the following commands.
Then use this command to disable swap:
You can confirm that it's now empty:
And to re-enable it:
And now reconfirm with
free
: