Manually clearing the memory cache in Linux

If your Linux based computer is using a lot of memory and you which to clear the memory caches, then rebooting always helps. However, if you don’t want to reboot, luckily, there is a command that will fix this problem. Typically there is nothing to worry about since Linux is very good at handling memory.

First off, Linux aggressively uses memory for caching so that programs run faster. If another program needs to start and the memory is in use, Linux removes some of the cache to make room. To see what I am talking about, run the following command in your terminal or console program of choice:

free -m

Notice that your cache may take more than half of your memory. In my case, I have 8 GB of RAM and there are times where I only have 50 MB of RAM available. After I ran the command to clear the cache, here was my result (At the time I made this article, I had a VMware virtual machine running Windows 7; it was using 1.5 GB of RAM which explains the heavy amount of used RAM):

me@ubuntu:~$ free -m

total used free shared buffers cached
Mem: 7936 3046 4890 0 14 424
-/+ buffers/cache: 2606 5329
Swap: 1906 0 1906

To make this happen, open terminal and run su – (if you are on Ubuntu, first you have do run sudo passwd and create a root password).

Enter your root password and execute the following command:

sync; echo 3 > /proc/sys/vm/drop_caches

Now if you run free -m again, you will see that your memory is once again free.