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

Clearing RAM memory cache, buffers, and swap space on Linux can sometimes be necessary for specific purposes like testing, debugging, or troubleshooting performance issues. However, it’s important to note that clearing these caches might affect system performance temporarily, and in most cases, the Linux kernel is efficient at managing these resources.

Here in this article, we will explore how to clear RAM memory cache, buffer, and swap space on a Linux system to enhance overall performance.

Understanding RAM Memory Cache, Buffer, and Swap Space

Let’s explore RAM memory cache, buffer, and swap space on a Linux system.

RAM Memory Cache

The RAM memory cache is a mechanism used by the kernel to keep regularly accessed data. While this boosts system responsiveness, an overloaded cache can lead to the retention of obsolete data, affecting performance.

Buffer

Like cache, the buffer holds data temporarily but differs in its purpose. Buffers store data being transferred between components like the CPU and hard disk, facilitating smooth communication. However, an excess of buffered data can hinder system speed.

Swap Space

Swap space is an allocated area on the hard disk that acts as virtual memory when physical RAM is exhausted. While it prevents system crashes due to low memory, it can slow down the system if overused.

Clearing Cache, Buffer, and Swap Space in Linux

In certain situations, you may need to clear the cachebuffer, or swap space as explained below.

How to Clear RAM Memory Cache in Linux?

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

Clearing PageCache

1. To clear the PageCache only, you can use the following command, which will specifically clear the PageCache, helping to free up memory resources.

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

Clearing Dentries and Inodes

2. To clear the dentries and inodes only, you can use the following command, which will sync the filesystem and clear both dentries and inodes, improving system performance by releasing cached directory and inode information.

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

Clearing PageCache, Dentries, and Inodes

3. To clear the pagecachedentries, and inodes, you can use the following command, which will sync the filesystem and clear the pagecache, dentries, and inodes, helping to free up memory and improve system performance.

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

Here’s an explanation of each part of the above command:

  • The sudo is used to execute the command as a superuser.
  • The sync will flush the file system buffer.
  • The “;” semicolon is used to separate multiple commands on a single line.
  • The echo 3 > /proc/sys/vm/drop_caches command is used to drop the page cache, a temporary storage area for recently accessed files.

Note: The drop_caches file controls which type of cached data should be cleared and the values are as follows:

  • 1 – Clears only the page cache.
  • 2 – Clears dentries and inodes.
  • 3 – Clears page cache, dentries, and inodes.

As mentioned in the kernel documentation, writing to drop_caches will clean the cache without killing any application/service, command echo is doing the job of writing to the 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 pagecachedentries, and inodes.

How to Clear Swap Space in Linux?

To clear swap space, you can use the swapoff command with the -a option, which will disable all swap partitions.

sudo swapoff -a

Then, turn it back by running the following command, which will activate all swap partitions.

sudo swapon -a

Is It Advisable to Free Buffer, Cache, and Swap in Linux?

In general, it is not a good idea to manually free up Buffer and Cache in Linux that might be used by the Linux kernel, which is designed to manage these resources efficiently, and manually clearing them can disrupt system performance.

However, there may be rare situations where you need to clear the Buffer and Cache, such as if you are experiencing severe memory pressure and cannot free up memory by other means. In these cases, you should proceed with caution and be aware of the potential performance impact.

Similarly, clearing swap space in Linux is generally not a routine or advisable practice under normal circumstances.