How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

Like any other operating system, GNU/Linux has implemented memory management efficiently and even more than that. But if any process is eating away at your memory and you want to clear it, Linux provides a way to flush or clear the RAM cache.

How to Clear Cache in Linux?

Every Linux System has three options to clear the cache without interrupting any processes or services.

1. Clear PageCache only.

# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, entries, and inodes.

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

Explanation Of The Above Command

Sync will flush the file system buffer. Commands separated by “;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in the kernel documentation, writing to drop_cache will clean the cache without killing any application/service, command echo is doing the job of writing to a file.

If you have to clear the disk cache, the first command is safest in enterprise and production, as “...echo 1 > ….” will clear the PageCache only. It is not recommended to use the third option above, “...echo 3 >” in production until you know what you are doing, as it will clear PageCache, entries, and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by the Linux Kernel?
When you are applying various settings and want to check if it is implemented, especially on an I/O-extensive benchmark, then you may need to clear the buffer cache. You can drop the cache as explained above without rebooting the System, i.e., no downtime required.

Linux is designed in such a way that it looks into the disk cache before looking at the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover, it will also slow the system for a few seconds while the cache is cleaned and every resource required by the OS is loaded again into the disk cache.

Now we will be creating a shell script to auto clear RAM cache after Every Hour via a cron scheduler task. Create a shell script, clearcache.sh, and add the following lines.

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production; instead, use "echo 1"
sync; echo 3 > /proc/sys/vm/drop_caches
Set execute permission on the clearcache.sh file.

# chmod 755 clearcache.sh

Now you may call the script whenever you need to clear the RAM cache.

Now set a cron to clear the RAM cache after Every Hour. Open crontab for editing.

# crontab -e

Append the below line, save, and exit to run it Every Hour.

0 * * * * /root/clearcache.sh

Is it a good idea to auto-clear clear RAM cache on a production server?

No! It is not. Think of a situation when you have scheduled the script to clear the RAM cache every hour. After every hour, the script is executed, and it flushes your RAM cache. One day, for whatever reason, maybe more than expected users are online on your website and seeking resources from your server.

At the same time scheduled script runs and clears everything in the cache. Now all the users are fetching data from disk. It will result in a server crash and corrupt the database. So clear the ram-cache only when required, and know your foot steps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the following command.

# swapoff -a && swapon -a

Also, you may add the above command to the cron script above, after understanding all the associated risks.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'

OR

$ su -c “echo 3 >’/proc/sys/vm/drop_caches’ && swapoff -a && swapon -a && printf ‘\n%s\n’ ‘Ram-cache and Swap Cleared'” root

After testing both above commands, we will run the command “free -h” before and after running the script, and will check the cache.

That’s all for now. If you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know what you think. Is it a good idea to clear the RAM cache and buffer in production and Enterprise?

We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies

More