Linux provides an efficient memory management mechanism that ensures optimal utilization of memory by various programs and processes. However, if a few applications are hogging huge chunks of RAM, you might want to clear the cache.
There are a number of ways of clearing the cache without killing running processes on the system. In this guide, we explore how you can empty the memory buffer and clear the cache in a Linux system.
Before we proceed further, let’s, first of all, see what a buffer and cache are.
What is Buffer and Cache Memory on Linux
In broader terms, the buffer is a memory area that temporarily stores data in transit, i.e. data that is being moved from one point to another. It contains metadata that helps boost write performance.
On the other hand, the cache is a temporary storage area that stores frequently accessed data for faster access. When data is stored in the cache, accessing it on subsequent occasions becomes much faster since data is retrieved from a cached copy rather than the original data.
Let us now see how to clear the memory buffer and cache in Linux.
Clear Disk Cache on Linux
Page cache also referred to as disk cache, is the cache used by the Linux kernel during disk reads and writes. To clear the page cache, execute the following command:
# sync; echo 1 > /proc/sys/vm/drop_caches
The drop_caches option cleans the cache without prematurely killing any applications on your Linux system. Just to verify that the cache was cleared, run the following command.
# free -h
Observe the difference between free memory before and after clearing the cache.
In the first instance, you can see that free memory is 342 MB. After clearing the page cache, this goes up to 1.3GB. In addition, notice that the buff/cache has been flushed from 1.3GB to 329MB.
Clear Dentries and Inodes Cache Memory on Linux
Dentries is a data structure that represents a directory or a folder. Dentries can be used to build or store a memory cache. Inodes are part of the Linux filesystem that stores metadata about a file. This includes file size, location, permissions, ownerships, etc.
To clear dentries and inodes, run the following command as the root user:
# sync; echo 2 > /proc/sys/vm/drop_caches'
Clear Pagecache, Dentries, and Inodes on Linux
To clear Pagecache (disk cache), dentries, and inodes in one command, simply run the following command.
# sync; echo 3 > /proc/sys/vm/drop_caches
Let’s take a minute and flesh out the above command options.
- All the above commands are segmented into two sections using the semi-colon
( ; )
that appears between sync and echo commands. - The sync option clears the file system buffer while the echo command writes to drop_cache and in so doing, cleans the cache without killing any application/service.
- Then back to the semi-colon, which instructs the shell to run the first command and once executed, run the next one. So, in this case, the sync command executes first, followed by the echo command.
NOTE: To clear the disk cache, the first command is safest in both enterprise and production environments as "...echo 1 > …."
clears the PageCache only.
The third option above "...echo 3 >"
is not recommended in production environments since it clears everything – page cache, dentries, and inodes – something which might break the system and impact production workloads.
The commands we have just seen are meant to be run as root. If you are running them as a sudo user, the syntax will change slightly as shown.
$ 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'
Is It a Good Idea to Free Buffer and Cache in Linux?
Linux periodically empties data from the cache in order to free up memory needed by other running programs. The cache is designed to dynamically fill up and clear to make room for more memory for other applications.
Clearing the cache has the tendency of slowing down disk reads by just a few seconds since the system will now be forced to fetch data from the disk instead of retrieving it from the cache. After a while, resources needed by the system will again be loaded into the cache.
As such, it is safe to clear the cache and buffer as this has little impact on your system’s performance.
How to Clear Swap Space in Linux?
Swap space is the area on the hard drive that is used when the RAM is almost getting depleted or exhausted. Under certain situations, you might be compelled to clear the swap space on your Linux PC.
To clear the swap space, simply run the following command as the root
# swapoff -a && swapon -a
You can clear both cache and swap space by combining them into a single command as shown.
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
Be sure to run the command free -h before and after running the script to confirm that RAM has been freed up.
# free -h
Conclusion
In this article, we have seen how to flush or clear the buffer and memory cache by sending an echo command to the /proc filesystem. Generally speaking, the Linux system routinely clears the cache whenever situations demand such an action to be performed. However, if you need to manually clear the cache and buffer, these instructions will come in handy.
That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments