How to Clear the Buffer, Cache, and Swap Memory in Linux

Clearing the buffer, cache, and swap memory in Linux can be done easily with a few simple commands.

If you’ve ever used the free -m command, you’re probably familiar with the output, which provides information about the system’s total memory, used memory, free memory, shared memory, and buffer/cache memory.

Apart from buffer/cache memory, all are self-explanatory, and we all know how to clear the memory that has been used, but I found there was some confusion around how to clear buffer/cache memory, so I decided to write an article about it.

Here we will discuss how to clear buffer/cache and swap memory in Linux, why it is important to do so, and the best methods to clear this type of memory.

How to Clear or Empty Buffer, Cache, and Swap Memory in Linux

As I previously stated, we all know how to clear used RAM space, but we rarely scratch the surface of what buffer and cache memory are and how to clear them in Linux, so before I show you the command, let me define buffer and cache memory, which will help you understand why we need to clear them.

What is Buffer and Cache Memory?

The terms buffer and cache memory are often used synonymously because they both serve the same function of storing frequently accessed data temporarily for quick access.

If we talk specifically about buffer memory, it is the part of the RAM that is used to temporarily store metadata information about files that are being read from or written to a disk, allowing the data to be accessed quickly without having to go through the slower disk I/O operations.

While cache memory is a form of fast access memory that stores recently used instructions and data (with the actual contents of files or block devices) so that it can be quickly accessed when the same data is requested.

I cannot explain the above abstract concept in greater detail here, so I recommend you read this article for a more complete picture of buffer/cache memory.

When you should manually clear buffers and cache memory

As you know, memory buffers and caches are used to store data and instructions so they can be accessed quickly. However, their size is limited, so Linux is designed in such a way that it tries to keep as many of the most recently used instructions and data in the memory buffers and caches as possible by removing old data and instructions that haven’t been used in a while.

But sometimes you need to manually clear the buffers and caches in order to free up space for other memory-intensive processes.

To do so, you can use a set of commands that will force the system to clear out the memory buffers and caches and reset them to their default states without affecting any of the other processes currently running.

You will first see how to clear the disc cache, then how to clear the dentries and inodes cache, and finally how to clear the page cache, dentries, and inodes on Linux.

Clear Page Cache or Disk Cache on Linux

I would suggest you first check the current status of memory by executing the below command, which will help you get an idea of how much free memory is available and also how much buffer and cache memory are currently being used by the system.

$ free -h

The output of the above command is “Show me 3 GB of space that is occupied by buffer/cache memory, so now let free some space by running the next command.”

Buffer memory stats
Buffer memory stats

To clear page cache or disk cache on Linux, you can use the below command, which will clear all of the system’s page cache (used by the Linux kernel during disk reads and writes) without affecting the buffer cache.

$ sudo sync; echo 1 | sudo tee /proc/sys/vm/drop_caches
  • sudo sync: command is used to synchronise the data in memory with that on disk, ensuring any changes to disk are committed before flushing them out.
  • echo 1: parameter followed by the sync command can be used to clear the page cache.
  • sudo tee: command will write the data provided by the previous command to /proc/sys/vm/drop_caches.

To get the result of the above command, execute free -m, which will print the memory usage of the system in MB.

Result shows the output of the above command
Result shows the output of the above command

Command to Clear Dentries and Inodes Cache

You already learned how to clear page cache with echo 1, so now you’ll use echo 2 to clear dentries (which are data entries in a computer’s filesystem that refer to both directories and files) and inodes.

To do this, run the below command:

$ sudo sync; echo 2 | sudo tee /proc/sys/vm/drop_caches

Command to Clear out Page Cache, Dentries, and Inodes

The next command will remove everything like page cache, dentries, and inodes, which are not recommended to run on production servers unless you are aware of the impact, but you can run it to free up memory on your Linux server.

So to clear page cache, dentries, and inodes on a Linux server, run the following command:

$ sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

Command to Clear out Swap Memory

There might be a situation where you need to clear up the swap memory to free up more memory on the system. In that case, you can use the below command, which will deallocate the swap memory and allow you to use the system resources effectively.

Also see: What Is the Difference Between Su, Sudo Su, Sudo -i, and Sudo -s?

For this, you need to switch your account to root and execute the command.

# swapoff -a && swapon -a

This command is a powerful way to clear up the swap memory and free up RAM on the system.

Wrap up

That’s all for this article, where you learn how to clear the buffer cache and swap memory. You can also configure the above command as an alias or short script to run automatically when needed using the curl command.

Also See: Virtual Memory Statistics with vmstat, which is an important tool for administrators and Linux enthusiasts.

If you have any questions or comments, please feel free to leave them in the comments section below.

Till then, put your feet up and relax. As always, the next article will be up shortly.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments