If you have ever kept your Linux machine running for extended periods of time (months), you will begin to notice that your system increasingly uses the swap partition with the cached area growing. Linux stores caches, dentries and inodes within memory to speed up reads and writes of the more commonly and recently used data segments. In doing so, this increases speed and efficiency for the applications you happen to be running. But wouldn’t it be nice to clear all of that from cache and ensure it is being placed in memory? Well guess what, you can!
Here is an example of my current system resources using the top command:
Mem: 2960104k total, 2884464k used, 75640k free, 325464k buffers
Swap: 2931820k total, 281840k used, 2649980k free, 344532k cached
As you can see my system is utilizing a lot of my swap space, and it would be nice to clear that and the cache out. This can be done if your kernel version is >= 2.6.16. A proc variable gives us the ability to clear specific portions, for instance:
- 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
To execute these commands you must be running as root or using the sudo command. It is also recommended to run the sync command prior to the aforementioned echo commands, sync will ensure that all cached objects are freed.
Executing the command on my system produces the following results:
# sync; echo 3 > /proc/sys/vm/drop_caches
Mem: 2960104k total, 2220148k used, 739956k free, 576k buffers
Swap: 2931820k total, 281628k used, 2650192k free, 144664k cached
Wow! Look at the memory buffer difference, and the amount of now free memory.
A lot of this information came from Linux Insight, check it out for a thorough discussion on its uses.