opened image

Working with Cache and Buffered Memory in Linux: Overview and Cleaning Guide

In Linux, the buffer memory functions as intermediate storage for data that is moved temporarily from one location to another. It contains metadata that improves the efficiency of write operations.

 

The cache, on the other hand, is a temporary repository that stores frequently accessed data. Storing data in a cache improves retrieval because it is taken from a cached copy, rather than from the original source.

Now let's find out how we can clear buffer memory and the cache in Linux.

 

Clearing the Disk Cache in Linux

The disk cache or page cache is used by the Linux kernel when it reads and writes to the disk. The following command is used to clear the page cache:  

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

 

The drop_caches option allows you to clear the cache without allowing applications running in the system to terminate prematurely. To test if the cache cleanup was successful, you can use:  

 free -h

 

Compare free memory figures to:

 

And after:

 

Clearing the Dentries and Inodes cache in Linux

 

Dentries – are a data structure that represents a directory or folder. Dentries can be used to create or store a cache in memory. Inodes – are a Linux filesystem element that stores metadata about a file, including file size, location, permissions, owners, etc.

Use the following command with root permissions to clean up dentries and inodes:

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

 

 

Clearing Pagecache, Dentries, and Inodes in Linux

 

You can use the following command to clear Pagecache (disk cache), dentries, and inodes at the same time.

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

 

 

Each of these commands is separated by a semicolon (;) between the sync and echo commands. The sync command clears the file system buffer, while the echo command writes to drop_cache, clearing the cache but not terminating the application or services.

But using "...echo 3 >" is not recommended in a production environment, because it clears all – page caches, dentries and inodes– which can cause a system crash and affect services.

 

If you are running as sudo, the command syntax will change slightly:

 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'

 

Should the buffer and cache be cleared in Linux?

 

The Linux system automatically cleans the cache periodically to free up memory that other running programs may need. Clearing the cache can make reading from the disk a bit slower, because the system needs to retrieve data directly from the disk and not from the cache. However, after a while, the system will load the resources it needs into the cache again. So, in most cases, clearing the cache and the buffer does not affect the performance of the system.

On servers in a production or enterprise environment, however, cache cleanup is not recommended, because it can decrease service performance.

 

Cleaning up Linux swap space

 

P Swap space – this is the area on your hard drive that you use when you run out of RAM. Sometimes you may need to clear swap space on your Linux computer. You can do so with the following command:

 swapoff -a && swapon -a

 

 

As a result, swap cleared:

 

If you want to clear both the cache and the swap area, you can use the following command:

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

 

Check the amount of free memory before and after you run the script to make sure RAM has been freed.

 free -h

.

 

Conclusion

 

In this article, we have looked in detail at how to clear buffered memory and cache in Linux by using the echo command directed to the /proc file system. Normally, the Linux system does the cache cleanup itself if it is required, but in some cases you may need to manually clear the cache and the buffer. In this case, you already know how to do it.